question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: unsupported operand type(s)

See original GitHub issue

Hi there. I have an edge-case where pyupgrade suggested breaking changes to my code, which I think is because it assumes that any usage of ** implies that it’s a dictionary and can be |'d with other dictionaries. I’m running pyupgrade with --py39-plus.

from collections.abc import Mapping


class A(Mapping):
    proxy = {"foo": 1}

    def __getitem__(self, k):
        return self.proxy.__getitem__(k)

    def __len__(self):
        return self.proxy.__len__()

    def __iter__(self):
        return self.proxy.__iter__()

a = A()
b = {"bar": 2}

# Basic assumption holds.
assert {**a} == {"foo": 1}

# Original code works.
assert {**a, **b} == {"foo": 1, "bar": 2}

# Pyupgrade suggestion
assert a | b == {"foo": 1, "bar": 2}
# TypeError: unsupported operand type(s) for |: 'A' and 'dict'

Is this an edge-case that Pyupgrade should handle, or is it safe to assume that any class that uses ** needs to implement compatibility with a dictionary union?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, Aug 27, 2021

which I think is because it assumes that any usage of ** implies that it’s a dictionary

the code that deals with this is here

https://github.com/asottile/pyupgrade/blob/aaad686b5f7851dd1b269f87cb5832ed132f4631/pyupgrade/_plugins/pep584.py

If it thinks it’s a dictionary, it’s because that’s what’s detected by the inbuilt ast module

Running astpretty on your code snippet, we get

                left=Dict(
                    lineno=29,
                    col_offset=7,
                    end_lineno=29,
                    end_col_offset=17,
                    keys=[
                        None,
                        None,
                    ],
                    values=[
                        Name(lineno=29, col_offset=10, end_lineno=29, end_col_offset=11, id='a', ctx=Load()),
                        Name(lineno=29, col_offset=15, end_lineno=29, end_col_offset=16, id='b', ctx=Load()),
                    ],
                ),

for {**a, **b}

0reactions
asottilecommented, Sep 17, 2021

@spookylukey please don’t mention that project here – the maintainer is not a kind member of open source and I do not want to give them free advertising

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: unsupported operand type(s) for -: 'str' and 'int'
The reason this is failing is because (Python 3) input returns a string. To convert it to an integer, use int(some_string) . You...
Read more >
TypeError: unsupported operand type(s) for /: str and int
The Python TypeError: unsupported operand type(s) for /: str and int occurs when we try to use the division `/` operator with a...
Read more >
TypeError: unsupported operand type(s) for -: 'str' and 'int'
This error occurs when you attempt to perform subtraction with a string variable and a numeric variable. The following example shows how to ......
Read more >
TypeError unsupported operand type(s) for + 'int' and 'str'
The concatenation operator “+” is used for this. But while doing so, a common error that is encountered is “TypeError unsupported operand type(s)...
Read more >
9/9 unsupported operand type(s) for ** | Codecademy
... in File "python", line 28, in grades_std_deviation TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'float' what's wrong in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found