-
-
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
functools.partial support #1484
Comments
The error message is generated because a class with a Also, |
You mean that the |
I meant that mypy would have to special case |
Maybe this could be supported by #3299 |
Yes, this looks like a good candidate for function plugins. Increasing priority since this is may be pretty easy to implement now. Just supporting positional arguments for |
This particular case and probably other basic use cases are already supported by protocols (see tests in PR #3132), simply because |
Protocols won't help with inferring the argument types of the result, though? |
Yes, I didn't do any special-casing, |
The original example is now fixed by #3132. This issue can be kept open to track better support of |
Not sure if this has been fixed entirely or perhaps it's a different issue:
|
I think this crops up also when trying to use a
|
python/typeshed#2878 changed how |
NOTE: Usage of partials has been removed due to typing issues More details could be found on python/mypy#1484
NOTE: Usage of partials has been removed due to typing issues More details could be found on python/mypy#1484
+1 for supporting |
I stumbled across this: from typing import reveal_type
from functools import partial
class A:
pass
p = partial(A)
reveal_type(p) # Revealed type is "functools.partial[A]"
reveal_type(p.func) # Revealed type is "def (*Any, **Any) -> A"
print(issubclass(p.func, A))
# error: Argument 1 to "issubclass" has incompatible type "Callable[..., A]"; expected "type" [arg-type] I don't know if this is considered a mypy bug or even related to partials, but the code runs fine, and |
It's because in the typeshed, you have class partial(Generic[_T]):
func: Callable[..., _T] So, the class is transformed into a callable on assignment. MyPy is doing the right thing here. |
I see. |
Yes, assigning to a member of a class can lose type information. Just like This issue is about partial matching callable. |
Should this be a separate issue? A partial of a type is not recognised as a type:
Admittedly, this could also be not type-safe:
Not sure if there's a better way to handle this. My actual use case is to accept any subclass of an abstract type, but some implementations may need positional arguments and some not, so I use partial to include the positional arguments needed before passing into the function. |
@Dreamsorcerer, I don't think your example above should type check. In other words, I think mypy is doing the right thing because def foo(a: Callable[..., int], **kwargs): ... |
Right, but then we lose all typing for arguments. My actual use case is:
Would be nice not to lose typing on the kwargs (and I get an explicit Any error too). |
Although, that's interesting that I can do So, actually, maybe the problem is that it allows a subclass of the abstract type to require an argument which the abstract class does not require... |
In Python, constructors don't obey LSP. That's why you shouldn't annotate the function with
You can type the arguments by providing them to |
Fixes python#1484 This is currently the most popular mypy issue that does not need a PEP. I'm sure there's stuff missing, but this should handle most cases.
Fixes python#1484 This is currently the most popular mypy issue that does not need a PEP. I'm sure there's stuff missing, but this should handle most cases.
Fixes #1484 Turns out that this is currently the second most popular mypy issue (and first most popular is a type system feature request that would need a PEP). I'm sure there's stuff missing, but this should handle most cases.
I haven't been able to find another issue for this, weird that this hasn't been reported already.
This will fail with
a similar code by using a dyadic
add
instead ofinc
will yield:(so, partial apparently doesn't currently retain any type information on the non-applied inputs... I guess this'll make the fix non trivial)
The text was updated successfully, but these errors were encountered: