Skip to content

Commit 6076488

Browse files
ddfishergvanrossum
authored andcommitted
Make operations on None fall back on object (#1835)
1 parent 79f4a4b commit 6076488

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

mypy/checkmember.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
from mypy import subtypes
1919

2020

21-
def analyze_member_access(name: str, typ: Type, node: Context, is_lvalue: bool,
21+
def analyze_member_access(name: str,
22+
typ: Type,
23+
node: Context,
24+
is_lvalue: bool,
2225
is_super: bool,
2326
builtin_type: Callable[[str], Instance],
2427
not_ready_callback: Callable[[str, Context], None],
25-
msg: MessageBuilder, override_info: TypeInfo = None,
28+
msg: MessageBuilder,
29+
override_info: TypeInfo = None,
2630
report_type: Type = None) -> Type:
2731
"""Analyse attribute access.
2832
@@ -72,6 +76,11 @@ def analyze_member_access(name: str, typ: Type, node: Context, is_lvalue: bool,
7276
elif isinstance(typ, AnyType):
7377
# The base object has dynamic type.
7478
return AnyType()
79+
elif isinstance(typ, NoneTyp):
80+
# The only attribute NoneType has are those it inherits from object
81+
return analyze_member_access(name, builtin_type('builtins.object'), node, is_lvalue,
82+
is_super, builtin_type, not_ready_callback, msg,
83+
report_type=report_type)
7584
elif isinstance(typ, UnionType):
7685
# The base object has dynamic type.
7786
msg.disable_type_names += 1

test-data/unit/check-expressions.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,3 +1493,11 @@ reveal_type("foo") # E: Argument 1 to "reveal_type" has incompatible type "str";
14931493
[case testRevealTypeVar]
14941494
reveal_type = 1
14951495
1 + "foo" # E: Unsupported operand types for + ("int" and "str")
1496+
1497+
[case testEqNone]
1498+
None == None
1499+
[builtins fixtures/ops.py]
1500+
1501+
[case testLtNone]
1502+
None < None # E: Unsupported left operand type for < (None)
1503+
[builtins fixtures/ops.py]

0 commit comments

Comments
 (0)