-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fix Callable
type to not be types.FunctionType
#614
Comments
Great idea!... but I'll suggest the scope of this issue be broadened to takle the actual problem once and for all. There's more than just Of course, I suppose users can manually define these but having to define the for every project can be a pain in the ass. Hence, the point of exporting such subtypes from If you deem this fit and eventually implement it, I'd suggest adding a note in the docs about the incompatibility of See also: python#14392 (comment) |
This comment was marked as outdated.
This comment was marked as outdated.
Callable
type to not have __name__
Callable
type to not be types.FunctionType
@daniil-berg @AnonymouX47 @helpmefindaname How does this proposal look? |
another issue caused by the same thing: # mypy: enable-error-code=redundant-expr
from types import MethodType, FunctionType
from typing import Callable
a: Callable[[], None]
if isinstance(a, FunctionType) or bool(): ... # error: Left operand of "or" is always false [redundant-expr]
if isinstance(a, MethodType) or bool(): ... # error: Left operand of "or" is always false [redundant-expr]
if isinstance(a, type) or bool(): ... # error: Left operand of "or" is always false [redundant-expr] |
Everythings looks okay to me except the last three types which I believe are not callable... instead, they're the types returned by their corresponding kinds of functions i.e Generator Function (of
Covering all those in the Sorry for the late reply. |
So we've got an open pr: With most of the work done already, not exactly what was discussed here. |
I have open a discuss to understand this feature with more clarity, #770 |
This on it's own would be fantastic and fix a huge hole in the type system. But maybe we can go further and create aliases and specializations for the callable types in
types
:FunctionType
LambdaType
- same asFunctionType
MethodType
BuiltinFunctionType
BuiltinMethodType
- same asBuiltinFunctionType
WrapperDescriptorType
MethodWrapperType
MethodDescriptorType
ClassMethodDescriptorType
The common attributes are:
['__name__', '__qualname__']
, maybe there would be value in predefining a type like:And the most useful one would be
FunctionType
with it's['__annotations__', '__closure__', '__code__', '__defaults__', '__get__', '__globals__', '__kwdefaults__', '__name__', '__qualname__']
.So we could introduce a
GenericAlias
forFunctionType
inbasedtyping
, it could also work with__future__.annotations
:Depending on how much work it is, we could just do all of them.
method binding /
__get__
function
defines__get__
which will bindself
with instances, currently mypy doesn't handle this correctly:I think that there is a larger problem of descriptors on subtypes breaking LSP(here), so even if we made this change, I don't think this aspect would be resolved. But if we used
FunctionType
here the error could be appeared correctly:(The semantics are up for discussion, but you would at least get some kind of error in this scenario)
The team has discussed this issue, and we are inclined to agree that
FunctionType
is not a subtype ofCallable
, because it's__get__
can returnMethodType
, which breaks LSP.This is only a problem on assignment to the class, so it is fine like:
We propose a new type alias
CallableType
be added tobasedtyping
with a value ofCallable | FunctionType
.The text was updated successfully, but these errors were encountered: