The reason will be displayed to describe this comment to others. Learn more.
I agree with other comments that using bound_args doesn't look like something we want to do. I think storing the real self type in a new attribute might be a better approach. All type operations should be updated to do something reasonable with the attribute.
It seems like the above should also fix the issue that no error is currently reported here, and perhaps other related issues:
class C:
def __new__(self) -> D:
return D()
class D(C):
x: int
C.x # No Error, but fails at runtime
We'd use use the new attribute when looking up class attributes.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
All reactions
bound_arg = get_proper_type(item.bound_args[0])
if isinstance(bound_arg, Instance) and isinstance(ret_type, Instance):
# Unfortunately, generic arguments have already been determined for us. We need these,
# see e.g. testGenericClassMethodUnboundOnClass. So just copy them over to our type.
# This does the wrong thing with custom __new__, see testNewReturnType15, but is
if you change checkexpr.py's apply_generic_arguments to expand bound_args (not sure if it's correct to do...):
return callable.copy_modified(
ret_type=expand_type(callable.ret_type, id_to_type),
+ bound_args=[+ expand_type(ba, id_to_type) if ba is not None else None+ for ba in callable.bound_args+ ],
variables=remaining_tvars,
type_guard=type_guard,
)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
def __new__(cls) -> int: # E: Incompatible return type for "__new__" (returns "int", but must return a subtype of "A")
pass
reveal_type(A()) # N: Revealed type is "__main__.A"
reveal_type(A()) # N: Revealed type is "builtins.int"
[case testNewReturnType4]
from typing import TypeVar, Type
Expand DownExpand Up
@@ -6993,6 +6993,54 @@ class MyMetaClass(type):
class MyClass(metaclass=MyMetaClass):
pass
[case testNewReturnType13]
from typing import Protocol
class Foo(Protocol):
def foo(self) -> str: ...
class A:
def __new__(cls) -> Foo: ... # E: Incompatible return type for "__new__" (returns "Foo", but must return a subtype of "A")
reveal_type(A()) # N: Revealed type is "__main__.Foo"
reveal_type(A().foo()) # N: Revealed type is "builtins.str"
[case testNewReturnType14]
from __future__ import annotations
class A:
def __new__(cls) -> int: raise # E: Incompatible return type for "__new__" (returns "int", but must return a subtype of "A")
class B(A):
@classmethod
def foo(cls) -> int: raise
reveal_type(B.foo()) # N: Revealed type is "builtins.int"
[builtins fixtures/classmethod.pyi]
[case testNewReturnType15]
from typing import Generic, Type, TypeVar
T = TypeVar("T")
class A(Generic[T]):
def __new__(cls) -> B[int]: ...
@classmethod
def foo(cls: Type[T]) -> T: ...
class B(A[T]): ...
# These are incorrect, should be Any and str
# See https://github.com/python/mypy/pull/16020
reveal_type(B.foo()) # N: Revealed type is "builtins.int"
reveal_type(B[str].foo()) # N: Revealed type is "builtins.int"
class C(A[str]): ...
reveal_type(C.foo()) # N: Revealed type is "builtins.str"
[builtins fixtures/classmethod.pyi]
[case testMetaclassPlaceholderNode]
from sympy.assumptions import ManagedProperties
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.