-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic Type[T] function argument rejected as base class #14458
Labels
bug
mypy got something wrong
Comments
I think the following similar example using from typing import Protocol, Type
class P(Protocol):
pass
def f(typ: Type[P]) -> Type[P]:
class C(typ):
pass
return C I'm very new to this though, so I'm not sure. |
From this comment on #5865, this still fails from typing import TypeVar
C = TypeVar("C", bound="MyClass")
class MyClass:
class InnerClass:
pass
@classmethod
def dynamic_subclass(cls: type[C]) -> type[C]:
# Mypy emits two errors on the following line:
# error: Variable "cls" is not valid as a type [valid-type]
# error: Invalid base class "cls" [misc]
class MySubClass(cls):
# Mypy emits one error on the following line:
# Name "cls.InnerClass" is not defined [name-defined]
class MySubInnerClass(cls.InnerClass):
pass
pass
return MySubClass
# to show that everything works as expected at runtime:
SubClass = MyClass.dynamic_subclass()
my_subclass_instance = SubClass() with errors ../../test.py:15: error: Variable "cls" is not valid as a type [valid-type]
../../test.py:15: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
../../test.py:15: error: Invalid base class "cls" [misc]
../../test.py:18: error: Name "cls.InnerClass" is not defined [name-defined]
Found 3 errors in 1 file (checked 1 source file) From what I can tell, these errors are related to this issue. |
erictraut
pushed a commit
to microsoft/pyright
that referenced
this issue
May 15, 2023
…ic base class in a `class` statement where the dynamic base class is based on a bound type variable. This addresses a bug that was reported in the mypy issue tracker: python/mypy#14458.
Also, example. from typing import cast, TypeVar
T = TypeVar('T')
def cast_as(cls: type[T], a) -> T:
return cast(cls, a) OYSL.py:7: error: Variable "cls" is not valid as a type [valid-type]
OYSL.py:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
Since #5865 was fixed, the following code now type checks:
However if we make the superclass generic, it doesn't type check anymore which I would expect it to do.
To Reproduce
Actual Behavior
Your Environment
1.0.0+dev.e9f5858c44b61c2d02819940debe3c31d099f9d5
mypy mypy_return_type.py
mypy.ini
(and other config files): None3.10.9
The text was updated successfully, but these errors were encountered: