-
-
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
None
alias for NoneType
doesn't work on generic bound when using type
of the generic
#11275
Comments
from typing import TypeVar, Generic
from types import NoneType
T = TypeVar("T", bound=None)
def foo(a: T, b: type[T]) -> None: ...
U = TypeVar("U", bound=NoneType)
def bar(a: U, b: type[U]) -> None: ...
foo(None, NoneType) #Value of type variable "T" of "foo" cannot be "Optional[NoneType]"
bar(None, NoneType) #Value of type variable "U" of "bar" cannot be "Optional[NoneType]" This is absolute whack |
I think this might be related to the fact that the stubs define |
wait, not it's not, it's completely unrelated to @final
class NoneType:
def __bool__(self) -> Literal[False]: ... LOOOOOOL!! |
I wouldn't expect this to work. The |
at runtime |
T = TypeVar("T", bound=type(None)) # error: TypeVar "bound" must be a type |
from typing import TypeVar
T = TypeVar("T", bound=None)
def foo(value: type[T]) -> None:
...
U = TypeVar("U", bound=type[None])
def bar(value: type[U]) -> None:
...
foo(type(None))
bar(type(None)) # Error |
|
What a mess lmao. in a type context, |
I think that we need better docs for from types import FunctionType
def a(): pass
b: FunctionType = a It also does not work. It should be using So, I will send a PR with some docs on why it is not support and what to do instead. |
The original example doesn't make much sense to me: None cannot be subclassed, so bounding on None or NoneType isn't useful. |
@JelleZijlstra it's just a minimal example. My actual use case is a class with generics bounded to a union of some class and T = TypeVar("T", bound=Foo | NoneType)
class Bar(Generic[T]):
t: type[T]
bar = Bar(NoneType) While trying to do this was when I found out that |
Pretty sure this would fix things: python/typeshed#6125 |
https://mypy-play.net/?mypy=latest&python=3.10&gist=ce1d20fa9232f609b36053e10a117a8b
The text was updated successfully, but these errors were encountered: