-
-
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
Type alias expansion sometimes interferes with type variable binding #3924
Comments
Sounds like the code that made |
I have an issue which is, I believe, related to this: from typing import TypeVar, Callable
T = TypeVar("T")
Function = Callable[[T], T]
a: Function = ...
b: Function[T] = ...
c: Callable[[T], T] = ... (save as Mypy always disallows How much is this related, should I file a separate issue? I would expect either When I was looking for duplicates, #8922 also looked related |
unfortunately the alias doesn't work see e.g. python/mypy#3924 ``` from typing import Any, Callable, TypeVar TCallable = TypeVar("TCallable", bound=Callable[..., Any]) Decorator = Callable[[TCallable], TCallable] def direct() -> Callable[[TCallable], TCallable]: ... def alias() -> Decorator[TCallable]: ... def half_alias() -> Decorator: ... reveal_type(direct()) reveal_type(alias()) reveal_type(half_alias()) ``` ``` $ mypy --strict test.py test.py:15:21: error: Missing type parameters for generic type "Decorator" test.py:19:13: note: Revealed type is "def [TCallable <: def (*Any, **Any) -> Any] (TCallable`-1) -> TCallable`-1" test.py:20:13: note: Revealed type is "def (<nothing>) -> <nothing>" test.py:21:13: note: Revealed type is "def (Any) -> Any" Found 1 error in 1 file (checked 1 source file) ```
I just closed #8273 as a duplicate of this issue, but there was some interesting discussion about possible causes and solutions in that issue. |
unfortunately the alias doesn't work see e.g. python/mypy#3924 ``` from typing import Any, Callable, TypeVar TCallable = TypeVar("TCallable", bound=Callable[..., Any]) Decorator = Callable[[TCallable], TCallable] def direct() -> Callable[[TCallable], TCallable]: ... def alias() -> Decorator[TCallable]: ... def half_alias() -> Decorator: ... reveal_type(direct()) reveal_type(alias()) reveal_type(half_alias()) ``` ``` $ mypy --strict test.py test.py:15:21: error: Missing type parameters for generic type "Decorator" test.py:19:13: note: Revealed type is "def [TCallable <: def (*Any, **Any) -> Any] (TCallable`-1) -> TCallable`-1" test.py:20:13: note: Revealed type is "def (<nothing>) -> <nothing>" test.py:21:13: note: Revealed type is "def (Any) -> Any" Found 1 error in 1 file (checked 1 source file) ```
Coming at this from #13449, where there's some discussion of my case (slightly different from the OP there), I have a question: Is there a benefit or meaning to allowing The only instances of |
|
unfortunately the alias doesn't work see e.g. python/mypy#3924 ``` from typing import Any, Callable, TypeVar TCallable = TypeVar("TCallable", bound=Callable[..., Any]) Decorator = Callable[[TCallable], TCallable] def direct() -> Callable[[TCallable], TCallable]: ... def alias() -> Decorator[TCallable]: ... def half_alias() -> Decorator: ... reveal_type(direct()) reveal_type(alias()) reveal_type(half_alias()) ``` ``` $ mypy --strict test.py test.py:15:21: error: Missing type parameters for generic type "Decorator" test.py:19:13: note: Revealed type is "def [TCallable <: def (*Any, **Any) -> Any] (TCallable`-1) -> TCallable`-1" test.py:20:13: note: Revealed type is "def (<nothing>) -> <nothing>" test.py:21:13: note: Revealed type is "def (Any) -> Any" Found 1 error in 1 file (checked 1 source file) ```
Any update on this? This is interfering with django ninja api decorator type hints (getting the following error: error: Untyped decorator makes function) when defining endpoints as: router:: Router = Router()
@router.get(
"endpoint",
response={
200: SomeModel,
400: Error,
401: Error,
404: Error,
500: Error,
},
)
def some_endpoint(request: HttpRequest -> SomeModel:
pass |
Consider the following two examples, I think they should be equivalent, since logically expansion of type aliases should happen before any other steps. (At least this would be consistent and natural for those familiar with C macros):
Here
test1
works as expected, while if I use an alias something strange happens withtest2
. Namely notice a subtle difference between their types.I have noticed few similar scenarios, they all seem to be related to
Callable
.The text was updated successfully, but these errors were encountered: