-
-
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
no error when contextmanager
decorator is above staticmethod
decorator
#15911
Comments
I'm not sure how mypy could catch this error without special-casing |
@AlexWaygood i still think these kind of errors are mypy's responsibility. if you look at any other language, things like this are usually implemented using keywords rather than decorators. and their type checkers will complain if you put them in the wrong order. take typescript for example: declare class Foo {
static public foo(): void // error: 'public' modifier must precede 'static' modifier.
} |
Mypy is doing the right thing here based on the type information it's given. I don't think this is a bug in mypy. At best, it's a bug in typeshed or a missing feature in the type system. I agree with Alex that this isn't an issue that deserves special-casing in the type checker logic because it's not a common source of bugs. This isn't at all similar to the TypeScript example because |
I don't think its considered a syntax error because it can be suppressed, still compiles and runs with no runtime error. So in a way typescript is even less obligated to complain than mypy is. I don't understand the inconsistent approach around special casing. For example |
also, why does mypy complain about decorators in the wrong order in some cases but not others? from abc import abstractmethod
from typing import Callable, TypeVar
Fn = TypeVar("Fn", bound=Callable[..., object])
def decorator(fn: Fn) -> Fn:
return fn
class Foo:
@abstractmethod # no error
@property
def foo(self) -> int:
return 1
@decorator # error: Decorators on top of @property are not supported [misc]
@property
def bar(self) -> int:
return 1 either it should be consistent and have errors for all decorators, or none at all. the current behavior is extremely confusing |
Interestingly, if we re-implement So it seems that mypys dedicated special-casing of from collections.abc import Callable
from typing import Generic, ParamSpec, TypeVar, overload
P = ParamSpec("P")
T = TypeVar("T")
R_co = TypeVar("R_co", covariant=True)
def contextmanager(fn: Callable[P, R_co]) -> Callable[P, R_co]:
def func(*args: P.args, **kwargs: P.kwargs) -> R_co:
return fn(*args, **kwargs)
return func
class static(Generic[P, R_co]):
@property
def __func__(self) -> Callable[P, R_co]: ...
@property
def __isabstractmethod__(self) -> bool: ...
def __init__(self, __f: Callable[P, R_co]) -> None: ...
@overload
def __get__(self, __instance: None, __owner: type) -> Callable[P, R_co]: ...
@overload
def __get__(self, __instance: T, __owner: type[T] | None = None) -> Callable[P, R_co]: ...
__name__: str
__qualname__: str
@property
def __wrapped__(self) -> Callable[P, R_co]: ...
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...
class Foo:
@contextmanager
@static
def foo() -> None: ...
@contextmanager
@staticmethod
def bar() -> None: ...
Foo().foo() # error: Attribute function "foo" with type "Callable[[], None]" does not accept self argument [misc]
Foo().bar() # no error |
playground
at runtime the following error occurs:
The text was updated successfully, but these errors were encountered: