TypeError: unsupported operand type(s)
See original GitHub issueHi 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:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
moduleRunning
astpretty
on your code snippet, we getfor
{**a, **b}
@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