You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm ... not quite sure that should be allowed. Going strictly by the specification:
Type checkers should assume that type narrowing should be applied to the expression that is passed as the first positional argument to a user-defined type guard.
(PEP 647, emphasis my own)
Notice how it says first and argument (no s). (and the "passed as" sets up that we're talking about function signature, not how the function is called)
Anyways, given that:
other type checkers support it
but it doesn't seem widely used (there's been no issue on here AFAICT about it)
it's not part of the specification
it's not clear how it should extend (first *args argument (like pyright)? all of them (which would allow more flexibility)?)
Should this even be attempted in the first place? I'm in favor of removing the TODO.
I have to admit that I hadn't considered the use of an *args parameter in a user-defined type guard function when I wrote PEP 647. I think it's ill-advised to implement a type guard function that starts with an *args because it's ambiguous what the behavior should be — or what the intent of the function's author was.
Pyright's implementation happens to handle this case. If it's simple for this to be fixed in mypy, then it might be worth making the change. I think it's also fine for mypy's maintainers to say "this is undefined behavior, so don't use *args as the first parameter to a user-defined type guard function".
Alright. I suspect it would be somewhat simple to handle, but will have to check in a while -- it's very likely, though, that I'll end up leaving the TODO alone. I'll leave this issue open in case someone with actual say in mypy wants to leave their opinion :P
Activity
A5rocks commentedon Dec 13, 2022
cc @erictraut as creator of pyright and typeguard PEP writer.
erictraut commentedon Dec 13, 2022
I have to admit that I hadn't considered the use of an
*argsparameter in a user-defined type guard function when I wrote PEP 647. I think it's ill-advised to implement a type guard function that starts with an*argsbecause it's ambiguous what the behavior should be — or what the intent of the function's author was.Pyright's implementation happens to handle this case. If it's simple for this to be fixed in mypy, then it might be worth making the change. I think it's also fine for mypy's maintainers to say "this is undefined behavior, so don't use
*argsas the first parameter to a user-defined type guard function".A5rocks commentedon Dec 13, 2022
Alright. I suspect it would be somewhat simple to handle, but will have to check in a while -- it's very likely, though, that I'll end up leaving the TODO alone. I'll leave this issue open in case someone with actual say in mypy wants to leave their opinion :P