Skip to content

Commit c024540

Browse files
committed
Add property support as well
1 parent 09fdc9a commit c024540

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mypy/checker.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4400,9 +4400,9 @@ def set_inferred_type(self, var: Var, lvalue: Lvalue, type: Type) -> None:
44004400
refers to the variable (lvalue). If var is None, do nothing.
44014401
"""
44024402
if var and not self.current_node_deferred:
4403-
# TODO: should we also set 'is_ready = True' here?
44044403
var.type = type
44054404
var.is_inferred = True
4405+
var.is_ready = True
44064406
if var not in self.var_decl_frames:
44074407
# Used for the hack to improve optional type inference in conditionals
44084408
self.var_decl_frames[var] = {frame.id for frame in self.binder.frames}
@@ -4424,6 +4424,11 @@ def set_inferred_type(self, var: Var, lvalue: Lvalue, type: Type) -> None:
44244424
var.is_staticmethod = True
44254425
elif is_node_class(definition):
44264426
var.is_classmethod = True
4427+
elif is_property(definition):
4428+
var.is_property = True
4429+
if isinstance(p_type, Overloaded):
4430+
# TODO: in theory we can have a property with a deleter only.
4431+
var.is_settable_property = True
44274432

44284433
def set_inference_error_fallback_type(self, var: Var, lvalue: Lvalue, type: Type) -> None:
44294434
"""Store best known type for variable if type inference failed.
@@ -8801,6 +8806,8 @@ def is_static(func: FuncBase | Decorator) -> bool:
88018806

88028807

88038808
def is_property(defn: SymbolNode) -> bool:
8809+
if isinstance(defn, FuncDef):
8810+
return defn.is_property
88048811
if isinstance(defn, Decorator):
88058812
return defn.func.is_property
88068813
if isinstance(defn, OverloadedFuncDef):

test-data/unit/check-callable.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,26 @@ reveal_type(C.bar2) # N: Revealed type is "Overload(def (x: builtins.int) -> bu
614614
reveal_type(C().bar2) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
615615
[builtins fixtures/classmethod.pyi]
616616

617+
[case testPropertyAliasInClassBody]
618+
class A:
619+
@property
620+
def f(self) -> int: ...
621+
622+
g = f
623+
624+
@property
625+
def f2(self) -> int: ...
626+
@f2.setter
627+
def f2(self, val: int) -> None: ...
628+
629+
g2 = f2
630+
631+
reveal_type(A().g) # N: Revealed type is "builtins.int"
632+
reveal_type(A().g2) # N: Revealed type is "builtins.int"
633+
A().g2 = 1
634+
A().g2 = "no" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
635+
[builtins fixtures/property.pyi]
636+
617637
[case testCallableUnionCallback]
618638
from typing import Union, Callable, TypeVar
619639

0 commit comments

Comments
 (0)