-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
False positive E1126 (invalid-sequence-index) on generic type alias with forward ref #9908
Comments
Running this code raises a type error:
So this looks like a true positive, i.e. Pylint is behaving correctly by warning about an actual error. |
My apologies, you're right. I oversimplified my failure test case. It fails without PEP563 deferred evaluation of annotations. Try this one: # pylint: disable=missing-docstring
from __future__ import annotations
from typing import TypeAlias, TypeVar
T = TypeVar("T")
Alias1: TypeAlias = list[T]
Alias2: TypeAlias = "list[T]"
x1: Alias1[int] # ok
x2: Alias2[int] # <= triggers E1126 (invalid-sequence-index) To add motivation as to why one might want to do this, here's a slightly less contrived example that fails pylint validation the same way: # pylint: disable=missing-docstring,too-few-public-methods
from __future__ import annotations
from typing import Generic, TypeAlias, TypeVar
T = TypeVar("T")
MaybeWrapped: TypeAlias = "Wrapped[T] | T"
class Wrapped(Generic[T]):
value: T
def __init__(self, v: MaybeWrapped[T], /) -> None: # <= triggers E1126
self.value = v.value if isinstance(v, Wrapped) else v |
Thanks, I see the problem now. Looks like Pylint thinks the As a workaround, you could work around the bug by moving from __future__ import annotations
from typing import Generic, TypeAlias, TypeVar
T = TypeVar("T")
class Wrapped(Generic[T]):
value: T
def __init__(self, v: MaybeWrapped[T], /) -> None: # <= triggers E1126
self.value = v.value if isinstance(v, Wrapped) else v
MaybeWrapped: TypeAlias = Wrapped[T] | T Or in 3.12, you can avoid all that mess by using the from __future__ import annotations
type MaybeWrapped[T] = Wrapped[T] | T
class Wrapped[T]:
value: T
def __init__(self, v: MaybeWrapped[T], /) -> None:
self.value = v.value if isinstance(v, Wrapped) else v |
Bug description
Configuration
No response
Command used
Pylint output
Expected behavior
Should not emit warning.
I've also tested with pylint from the head of the main branch (eb33f8a).
Pylint version
OS / Environment
Ubuntu 22.04
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: