Skip to content

Commit a0fcf31

Browse files
committed
Add tests for class methods and static methods and fix warn_deprecated_overload_item for static methods
1 parent 7f3d73c commit a0fcf31

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7746,7 +7746,7 @@ def warn_deprecated_overload_item(
77467746
if isinstance(item, Decorator) and isinstance(
77477747
candidate := item.func.type, CallableType
77487748
):
7749-
if selftype is not None:
7749+
if (selftype is not None) and (not node.is_static):
77507750
candidate = bind_self(candidate, selftype)
77517751
if candidate == target:
77527752
self.warn_deprecated(item.func, context)

test-data/unit/check-deprecated.test

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ for i in a: # E: function __main__.A.__iter__ is deprecated: no iteration
377377
[builtins fixtures/tuple.pyi]
378378

379379

380-
[case testDeprecatedOverloadedMethods]
380+
[case testDeprecatedOverloadedInstanceMethods]
381381
# flags: --enable-error-code=deprecated
382382

383383
from typing import Iterator, Union
@@ -426,6 +426,122 @@ b.h("x") # E: function __main__.A.h is deprecated: use `h2` instead
426426
[builtins fixtures/tuple.pyi]
427427

428428

429+
[case testDeprecatedOverloadedClassMethods]
430+
# flags: --enable-error-code=deprecated
431+
432+
from typing import Iterator, Union
433+
from typing_extensions import deprecated, overload
434+
435+
class A:
436+
@overload
437+
@classmethod
438+
@deprecated("pass `str` instead")
439+
def f(self, v: int) -> None: ...
440+
@overload
441+
@classmethod
442+
def f(self, v: str) -> None: ...
443+
@classmethod
444+
def f(self, v: Union[int, str]) -> None: ...
445+
446+
@overload
447+
@classmethod
448+
def g(self, v: int) -> None: ...
449+
@overload
450+
@classmethod
451+
@deprecated("pass `int` instead")
452+
def g(self, v: str) -> None: ...
453+
@classmethod
454+
def g(self, v: Union[int, str]) -> None: ...
455+
456+
@overload
457+
@classmethod
458+
def h(self, v: int) -> A: ...
459+
@overload
460+
@classmethod
461+
def h(self, v: str) -> A: ...
462+
@deprecated("use `h2` instead")
463+
@classmethod
464+
def h(self, v: Union[int, str]) -> A: ...
465+
466+
class B(A): ...
467+
468+
a = A()
469+
a.f(1) # E: overload def (self: type[__main__.A], v: builtins.int) of function __main__.A.f is deprecated: pass `str` instead
470+
a.f("x")
471+
a.g(1)
472+
a.g("x") # E: overload def (self: type[__main__.A], v: builtins.str) of function __main__.A.g is deprecated: pass `int` instead
473+
a.h(1) # E: function __main__.A.h is deprecated: use `h2` instead
474+
a.h("x") # E: function __main__.A.h is deprecated: use `h2` instead
475+
476+
b = B()
477+
b.f(1) # E: overload def (self: type[__main__.A], v: builtins.int) of function __main__.A.f is deprecated: pass `str` instead
478+
b.f("x")
479+
b.g(1)
480+
b.g("x") # E: overload def (self: type[__main__.A], v: builtins.str) of function __main__.A.g is deprecated: pass `int` instead
481+
b.h(1) # E: function __main__.A.h is deprecated: use `h2` instead
482+
b.h("x") # E: function __main__.A.h is deprecated: use `h2` instead
483+
484+
[builtins fixtures/tuple.pyi]
485+
486+
487+
[case testDeprecatedOverloadedStaticMethods]
488+
# flags: --enable-error-code=deprecated
489+
490+
from typing import Iterator, Union
491+
from typing_extensions import deprecated, overload
492+
493+
class A:
494+
@overload
495+
@staticmethod
496+
@deprecated("pass `str` instead")
497+
def f(v: int) -> None: ...
498+
@overload
499+
@staticmethod
500+
def f(v: str) -> None: ...
501+
@staticmethod
502+
def f(v: Union[int, str]) -> None: ...
503+
504+
@overload
505+
@staticmethod
506+
def g(v: int) -> None: ...
507+
@overload
508+
@staticmethod
509+
@deprecated("pass `int` instead")
510+
def g(v: str) -> None: ...
511+
@staticmethod
512+
def g(v: Union[int, str]) -> None: ...
513+
514+
@overload
515+
@staticmethod
516+
def h(v: int) -> A: ...
517+
@overload
518+
@staticmethod
519+
def h(v: str) -> A: ...
520+
@deprecated("use `h2` instead")
521+
@staticmethod
522+
def h(v: Union[int, str]) -> A: ...
523+
524+
class B(A): ...
525+
526+
a = A()
527+
a.f(1) # E: overload def (v: builtins.int) of function __main__.A.f is deprecated: pass `str` instead
528+
a.f("x")
529+
a.g(1)
530+
a.g("x") # E: overload def (v: builtins.str) of function __main__.A.g is deprecated: pass `int` instead
531+
a.h(1) # E: function __main__.A.h is deprecated: use `h2` instead
532+
a.h("x") # E: function __main__.A.h is deprecated: use `h2` instead
533+
534+
b = B()
535+
b.f(1) # E: overload def (v: builtins.int) of function __main__.A.f is deprecated: pass `str` instead
536+
b.f("x")
537+
b.g(1)
538+
b.g("x") # E: overload def (v: builtins.str) of function __main__.A.g is deprecated: pass `int` instead
539+
b.h(1) # E: function __main__.A.h is deprecated: use `h2` instead
540+
b.h("x") # E: function __main__.A.h is deprecated: use `h2` instead
541+
542+
[builtins fixtures/classmethod.pyi]
543+
544+
429545
[case testDeprecatedOverloadedSpecialMethods]
430546
# flags: --enable-error-code=deprecated
431547

0 commit comments

Comments
 (0)