Improve abc module and builtin function decorators (with ParamSpec)#5682
Improve abc module and builtin function decorators (with ParamSpec)#5682srittau wants to merge 12 commits into
Conversation
* Make classmethod and staticmethod generic over the function. * abstractclassmethod and abstractstaticmethod are classes, not functions. * Remove mypy-specific comments.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| def __init__(self, f: Callable[..., Any]) -> None: ... | ||
| def __init__(self, f: _FuncT) -> None: ... | ||
| def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... | ||
| def __get__(self, obj: _T, type: Optional[Type[_T]] = ...) -> Callable[..., Any]: ... | ||
| def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... |
There was a problem hiding this comment.
This doesn't seem right, because the callable returned from __get__ doesn't take the class as a first argument.
>>> class Foo:
... @classmethod
... def bar(cls):
... print("Bar runs:", cls)
...
>>> Foo.bar()
Bar runs: <class '__main__.Foo'>
>>> Foo.__dict__['bar'].__func__(Foo)
Bar runs: <class '__main__.Foo'>Here Foo.__dict__['bar'] is the classmethod object, and for Foo.bar, its __get__ method gets called. The result is a bound method, which basically means that it includes the class similarly to partial:
>>> Foo.bar == Foo.__dict__['bar'].__get__(Foo(), None)
True
>>> Foo.bar
<bound method Foo.bar of <class '__main__.Foo'>>There was a problem hiding this comment.
Another problem: I think it calls __get__(None, Foo) when looking up Foo.bar, so None should be allowed for the first argument to __get__.
There was a problem hiding this comment.
Good catch. I tried to express this using ParamSpec, but I'm not sure this is correct. Also, do you know in what circumstances the owner argument can be None or not set?
There was a problem hiding this comment.
It appears to be a technicality that doesn't really occur in practice: "Python’s own __getattribute__() implementation always passes in both arguments whether they are required or not." Docs
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
It seems that mypy doesn't support |
This comment has been minimized.
This comment has been minimized.
Akuli
left a comment
There was a problem hiding this comment.
The Concatenates look good to me, but I have never actually used Concatenate.
|
Diff from mypy_primer, showing the effect of this PR on open source code: freqtrade (https://github.com/freqtrade/freqtrade.git)
+ freqtrade/data/history/jsondatahandler.py:26: error: Signature of "ohlcv_get_available_data" incompatible with supertype "IDataHandler"
+ freqtrade/data/history/jsondatahandler.py:38: error: Signature of "ohlcv_get_pairs" incompatible with supertype "IDataHandler"
+ freqtrade/data/history/jsondatahandler.py:126: error: Signature of "trades_get_pairs" incompatible with supertype "IDataHandler"
+ freqtrade/data/history/hdf5datahandler.py:25: error: Signature of "ohlcv_get_available_data" incompatible with supertype "IDataHandler"
+ freqtrade/data/history/hdf5datahandler.py:37: error: Signature of "ohlcv_get_pairs" incompatible with supertype "IDataHandler"
+ freqtrade/data/history/hdf5datahandler.py:125: error: Signature of "trades_get_pairs" incompatible with supertype "IDataHandler"
graphql-core (https://github.com/graphql-python/graphql-core.git)
+ src/graphql/execution/execute.py:212: error: Incompatible types in assignment (expression has type "Callable[[Any], bool]", variable has type "Callable[[Arg(Any, 'value')], bool]")
pydantic (https://github.com/samuelcolvin/pydantic.git)
+ pydantic/class_validators.py:57: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:84: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:100: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:107: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:113: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:125: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:135: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/class_validators.py:328: error: Missing type parameters for generic type "classmethod" [type-arg]
+ pydantic/main.py:953: error: Missing type parameters for generic type "classmethod" [type-arg]
|
| def __dir__(self) -> Iterable[str]: ... | ||
| def __init_subclass__(cls) -> None: ... | ||
|
|
||
| class staticmethod(object): # Special, only valid as a decorator. |
There was a problem hiding this comment.
Nit: These comments should be deleted. They seem misleading and mypy-specific to me.
|
It's starting to LGTM, but I'm concerned about the new errors in freqtrade. They seem to be coming from usual |
|
staticmethod and classmethod are now generic on master. Do we still need the abc.pyi changes? |
|
Going to close this as stale for now to keep the list of open PRs manageable. If you're still interested in seeing this through, feel free to reopen or open a new PR. |
functions.