Skip to content

Bug: Remove Redundant Constructor in Dict Union when the other operand has to be cast as dict #265

Open
@Estrangeling

Description

@Estrangeling

I wrote this:

DIGITS = {str(i): i for i in range(10)} | dict(zip('ABCDEF', range(10, 16)))

In Visual Studio Code, I see this refactoring popping up:

Remove Redundant Constructor in Dict Union

The suggested fix was:

{str(i): i for i in range(10)} | zip('ABCDEF', range(10, 16))

Which raises the following error:

TypeError: unsupported operand type(s) for |: 'dict' and 'zip'

This is a bug, please fix this.

Side note:

Although I changed my code to the following:

DIGITS = {f'{i:X}' for i in range(16)}

I know I can use int(h, 16), but I need to get hexadecimal numbers out of a string that starts with hexadecimal but contains other things, I do it like this:

DIGITS = {f'{i:X}': i for i in range(16)}
i = n = 0
while (d := DIGITS.get(line[i])) is not None:
    n = (n << 4) | d
    i += 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions