Skip to content

Commit 1222c96

Browse files
srittauMichael0x2a
authored andcommitted
Don't treat functions with reversible dunder names as such (#5421)
Previously, functions that had the same name as a reversible dunder method (for example, __rpow__) were checked as if they were such a method. This could lead to crashes and false positives. Closes #5419
1 parent def2c63 commit 1222c96

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checker.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
775775
self.msg, context=fdef)
776776

777777
if name: # Special method names
778-
if name in nodes.reverse_op_method_set:
778+
if defn.info and name in nodes.reverse_op_method_set:
779779
self.check_reverse_op_method(item, typ, name, defn)
780780
elif name in ('__getattr__', '__getattribute__'):
781781
self.check_getattr_method(typ, defn, name)
@@ -976,6 +976,8 @@ def check_reverse_op_method(self, defn: FuncItem,
976976
# just decides whether it's worth calling
977977
# check_overlapping_op_methods().
978978

979+
assert defn.info is not None
980+
979981
# First check for a valid signature
980982
method_type = CallableType([AnyType(TypeOfAny.special_form),
981983
AnyType(TypeOfAny.special_form)],

test-data/unit/check-modules.test

+9
Original file line numberDiff line numberDiff line change
@@ -2495,3 +2495,12 @@ def __getattr__(attr: str) -> Any: ...
24952495
class Cls: ...
24962496
[builtins fixtures/module.pyi]
24972497
[out]
2498+
2499+
[case testFunctionWithDunderName]
2500+
def __add__(self) -> int: ...
2501+
2502+
[case testFunctionWithReversibleDunderName]
2503+
def __radd__(self) -> int: ...
2504+
2505+
[case testFunctionWithInPlaceDunderName]
2506+
def __iadd__(self) -> int: ...

0 commit comments

Comments
 (0)