GeezerVison is showing github.com — hacks · python/mypy@393b303
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: mypy/checkexpr.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3182,11 +3182,11 @@ def analyze_ordinary_member_access(self, e: MemberExpr, is_lvalue: bool) -> Type | 3182 | 3182 | member_type = analyze_member_access( | 3183 | 3183 | e.name, | 3184 | 3184 | original_type, | 3185 | | - e, | 3186 | | - is_lvalue, | 3187 | | - False, | 3188 | | - False, | 3189 | | - self.msg, | | 3185 | + context=e, | | 3186 | + is_lvalue=is_lvalue, | | 3187 | + is_super=False, | | 3188 | + is_operator=False, | | 3189 | + msg=self.msg, | 3190 | 3190 | original_type=original_type, | 3191 | 3191 | chk=self.chk, | 3192 | 3192 | in_literal_context=self.is_literal_context(), | |
Collapse file Copy file name to clipboardExpand all lines: mypy/checkmember.py +25-2Lines changed: 25 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -371,8 +371,27 @@ def validate_super_call(node: FuncBase, mx: MemberContext) -> None: | 371 | 371 | def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: MemberContext) -> Type: | 372 | 372 | # Class attribute. | 373 | 373 | # TODO super? | 374 | | - ret_type = typ.items[0].ret_type | | 374 | + item = typ.items[0] | | 375 | + ret_type = item.ret_type | 375 | 376 | assert isinstance(ret_type, ProperType) | | 377 | + | | 378 | + # The following is a hack. mypy often represents types as CallableType, where the signature of | | 379 | + # CallableType is determined by __new__ or __init__ of the type (this logic is in | | 380 | + # type_object_type). Then if we ever need the TypeInfo or an instance of the type, we fish | | 381 | + # around for the return type in CallableType.type_object. Unfortunately, this is incorrect if | | 382 | + # __new__ returns an unrelated type, but we can kind of salvage things by fishing around in | | 383 | + # CallableType.bound_args | | 384 | + self_type: Type = typ | | 385 | + if len(item.bound_args) == 1 and item.bound_args[0]: | | 386 | + bound_arg = get_proper_type(item.bound_args[0]) | | 387 | + if isinstance(bound_arg, Instance) and isinstance(ret_type, Instance): | | 388 | + # Unfortunately, generic arguments have already been determined for us. We need these, | | 389 | + # see e.g. testGenericClassMethodUnboundOnClass. So just copy them over to our type. | | 390 | + # This does the wrong thing with custom __new__, see testNewReturnType15, but is | | 391 | + # a lesser evil. | | 392 | + ret_type = bound_arg.copy_modified(args=ret_type.args) | | 393 | + self_type = TypeType(ret_type) | | 394 | + | 376 | 395 | if isinstance(ret_type, TupleType): | 377 | 396 | ret_type = tuple_fallback(ret_type) | 378 | 397 | if isinstance(ret_type, TypedDictType): | @@ -394,7 +413,11 @@ def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: Member | 394 | 413 | # See https://github.com/python/mypy/pull/1787 for more info. | 395 | 414 | # TODO: do not rely on same type variables being present in all constructor overloads. | 396 | 415 | result = analyze_class_attribute_access( | 397 | | - ret_type, name, mx, original_vars=typ.items[0].variables, mcs_fallback=typ.fallback | | 416 | + ret_type, | | 417 | + name, | | 418 | + mx.copy_modified(self_type=self_type), | | 419 | + original_vars=typ.items[0].variables, | | 420 | + mcs_fallback=typ.fallback, | 398 | 421 | ) | 399 | 422 | if result: | 400 | 423 | return result | |
Collapse file Copy file name to clipboardExpand all lines: mypy/typeops.py +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -202,10 +202,9 @@ def class_callable( | 202 | 202 | # this case (although we should probably change it at some point) | 203 | 203 | # See testValueTypeWithNewInParentClass | 204 | 204 | # Also see testSelfTypeInGenericClassUsedFromAnotherGenericClass1 | 205 | | - if ( | 206 | | - not is_subtype(default_ret_type, explicit_type, ignore_type_params=True) | 207 | | - or is_subtype(explicit_type, default_ret_type, ignore_type_params=True) | 208 | | - ): | | 205 | + if not is_subtype( | | 206 | + default_ret_type, explicit_type, ignore_type_params=True | | 207 | + ) or is_subtype(explicit_type, default_ret_type, ignore_type_params=True): | 209 | 208 | ret_type = explicit_type | 210 | 209 | elif ( | 211 | 210 | # We have to skip protocols, because it can be a subtype of a return type | |
Collapse file Copy file name to clipboardExpand all lines: mypy/types.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1466,7 +1466,7 @@ def deserialize(cls, data: JsonDict | str) -> Instance: | 1466 | 1466 | def copy_modified( | 1467 | 1467 | self, | 1468 | 1468 | *, | 1469 | | - args: Bogus[list[Type]] = _dummy, | | 1469 | + args: Bogus[Sequence[Type]] = _dummy, | 1470 | 1470 | last_known_value: Bogus[LiteralType | None] = _dummy, | 1471 | 1471 | ) -> Instance: | 1472 | 1472 | new = Instance( | |
Collapse file Copy file name to clipboardExpand all lines: test-data/unit/check-classes.test +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7007,6 +7007,41 @@ reveal_type(A()) # E: Cannot instantiate protocol class "Foo" \ | 7007 | 7007 | reveal_type(A().foo()) # E: Cannot instantiate protocol class "Foo" \ | 7008 | 7008 | # N: Revealed type is "builtins.str" | 7009 | 7009 |
| | 7010 | +[case testNewReturnType14] | | 7011 | +from __future__ import annotations | | 7012 | + | | 7013 | +class A: | | 7014 | + def __new__(cls) -> int: raise # E: Incompatible return type for "__new__" (returns "int", but must return a subtype of "A") | | 7015 | + | | 7016 | +class B(A): | | 7017 | + @classmethod | | 7018 | + def foo(cls) -> int: raise | | 7019 | + | | 7020 | +reveal_type(B.foo()) # N: Revealed type is "builtins.int" | | 7021 | +[builtins fixtures/classmethod.pyi] | | 7022 | + | | 7023 | +[case testNewReturnType15] | | 7024 | +from typing import Generic, Type, TypeVar | | 7025 | + | | 7026 | +T = TypeVar("T") | | 7027 | + | | 7028 | +class A(Generic[T]): | | 7029 | + def __new__(cls) -> B[int]: ... | | 7030 | + @classmethod | | 7031 | + def foo(cls: Type[T]) -> T: ... | | 7032 | + | | 7033 | +class B(A[T]): ... | | 7034 | + | | 7035 | +# These are incorrect, should be Any and str | | 7036 | +# See https://github.com/python/mypy/pull/16020 | | 7037 | +reveal_type(B.foo()) # N: Revealed type is "builtins.int" | | 7038 | +reveal_type(B[str].foo()) # N: Revealed type is "builtins.int" | | 7039 | + | | 7040 | +class C(A[str]): ... | | 7041 | + | | 7042 | +reveal_type(C.foo()) # N: Revealed type is "builtins.str" | | 7043 | +[builtins fixtures/classmethod.pyi] | | 7044 | + | 7010 | 7045 | [case testMetaclassPlaceholderNode] | 7011 | 7046 | from sympy.assumptions import ManagedProperties | 7012 | 7047 | from sympy.ops import AssocOp | |
|