-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
<nothing> is inferred when mimicking GADT #8252
Comments
In the example below of the same bug, after the
|
I also ran into this recently. From some more playing around with it, it seems like there's an issue when:
My minimal examples to show this: from typing import Any, Generic, TypeVar
T = TypeVar('T')
class Foo(Generic[T]):
def __init__(self, value: T):
self.value = value
def int_through_superclass(self) -> int:
if isinstance(self, IntFoo):
# error: <nothing> has no attribute "value"
return self.value
return -1
class IntFoo(Foo[int]):
def int_through_subclass(self) -> int:
# no issue
return self.value
def int_through_subclass_with_isinstance_check(self) -> int:
if isinstance(self, IntFoo):
# no issue
return self.value
return -1
def t_func(foo: Foo[T]) -> T:
if isinstance(foo, IntFoo):
# error: <nothing> has no attribute "value"
return foo.value
return foo.value
def int_func(foo: Foo) -> int:
if isinstance(foo, IntFoo):
# no issue
return foo.value
return -1
def int_t_func(foo: Foo[T]) -> int:
if isinstance(foo, IntFoo):
# error: <nothing> has no attribute "value"
return foo.value
return -1 There may be other cases (like methods in subclasses), but I didn't check them. |
Still opened and still an issue, I'd love being able to mimick GADTs in python |
While this issue came before, I'm closing this in favour of #12949 which describes the same bug in a much simpler way. Thanks for reporting! |
As mypy understands
isinstance
checks, I wondered if I can use this to mimic GADT (Generalized algebraic data type) in functional languages such as Haskell and OCaml.I tried to mimic following Haskell code
with the following Python code:
But in the
elif isinstance(e, Add)
branch the type ofe
is inferred as'<nothing>'
causing type errors.As
'<nothing>'
is not documented (c.f. #3030), I'm not sure if this is intended behavior. So I report it here just in case, though the attempt is just out of curiosity.The text was updated successfully, but these errors were encountered: