sourcery-vscode icon indicating copy to clipboard operation
sourcery-vscode copied to clipboard

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

Open Estrangeling opened this issue 8 months ago • 0 comments

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

Estrangeling avatar Feb 23 '25 11:02 Estrangeling