hasgeek / coaster

Common patterns for Flask apps
http://coaster.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
69 stars 13 forks source link

Fix decorator typing #462

Closed jace closed 4 months ago

jace commented 4 months ago

Solution found at https://github.com/python/typing/discussions/1040. Passes type checking in both Mypy and Pyright.

jace commented 4 months ago

Mypy is tripping on the WrappedFunc and ReturnDecorator types, casting them to Any. This affects the requires_roles and requestargs (and related) decorators, but a fix is deferred to another PR.

jace commented 4 months ago

Removing __get__ in the method protocol fixed return type detection, but somehow did not trigger the paramspec mismatch that we were stuck with earlier. Maybe a recent Mypy release fixed it, or maybe it was to do with the self parameter name in the protocol.

This does not work:

class Method(Protocol[P, R_co]):
    def __call__(self, __self: Any, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

However, this works:

class Method(Protocol[P, R_co]):
    def __call__(__self, self: Any, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

__get__ and the resulting bound variant of the protocol are not needed.