Skip to content

Commit

Permalink
prefix the warnings with class ..., function ..., or `overload ..…
Browse files Browse the repository at this point in the history
…. of function ...`
  • Loading branch information
tyralla committed Jul 5, 2024
1 parent 1372e66 commit 286371f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
15 changes: 9 additions & 6 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7562,15 +7562,18 @@ def create_deprecation_warning(
"""Create a warning when visiting a deprecated (decorated) callable, overload or
class that may be used later if the deprecated feature is used."""

name = typ.name
if isinstance(typ, CallableType):
if (defn := typ.definition) is not None:
name = defn.fullname
if (defn := typ.definition) is None:
name = f"function {typ.name}"
else:
name = f"function {defn.fullname}"
elif isinstance(typ, Overloaded):
if isinstance(func := typ.items[0].definition, FuncDef):
name = func.fullname
name = f"function {func.fullname}"
else:
name = f"function {typ.name}"
else:
name = typ.fullname
name = f"class {typ.fullname}"

overload = False
deprecation: str | None = None
Expand All @@ -7594,7 +7597,7 @@ class that may be used later if the deprecated feature is used."""

if deprecation is not None:
if overload:
typ.deprecated = f"{name} is deprecated [overload {typ}]: {deprecation}"
typ.deprecated = f"overload {typ} of {name} is deprecated: {deprecation}"
else:
typ.deprecated = f"{name} is deprecated: {deprecation}"

Expand Down
134 changes: 67 additions & 67 deletions test-data/unit/check-deprecated.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ from typing_extensions import deprecated
def f() -> None: ...

f() # type: ignore[deprecated]
f() # N: __main__.f is deprecated: use f2 instead [deprecated]
f() # N: function __main__.f is deprecated: use f2 instead [deprecated]

[builtins fixtures/tuple.pyi]

Expand All @@ -37,7 +37,7 @@ from typing_extensions import deprecated
def f() -> None: ...

f() # type: ignore[deprecated]
f() # E: __main__.f is deprecated: use f2 instead [deprecated]
f() # E: function __main__.f is deprecated: use f2 instead [deprecated]

[builtins fixtures/tuple.pyi]

Expand All @@ -49,14 +49,14 @@ from typing_extensions import deprecated
@deprecated("use f2 instead")
def f() -> None: ...

f # N: __main__.f is deprecated: use f2 instead # type: ignore[deprecated]
f(1) # N: __main__.f is deprecated: use f2 instead \
f # N: function __main__.f is deprecated: use f2 instead # type: ignore[deprecated]
f(1) # N: function __main__.f is deprecated: use f2 instead \
# E: Too many arguments for "f"
f[1] # N: __main__.f is deprecated: use f2 instead \
f[1] # N: function __main__.f is deprecated: use f2 instead \
# E: Value of type "Callable[[], None]" is not indexable
g = f # N: __main__.f is deprecated: use f2 instead
g() # N: __main__.f is deprecated: use f2 instead
t = (f, f, g) # N: __main__.f is deprecated: use f2 instead
g = f # N: function __main__.f is deprecated: use f2 instead
g() # N: function __main__.f is deprecated: use f2 instead
t = (f, f, g) # N: function __main__.f is deprecated: use f2 instead

[builtins fixtures/tuple.pyi]

Expand All @@ -67,17 +67,17 @@ import m
import p.s
import m as n
import p.s as ps
from m import f # N: m.f is deprecated: use f2 instead
from p.s import g # N: p.s.g is deprecated: use g2 instead
from m import f # N: function m.f is deprecated: use f2 instead
from p.s import g # N: function p.s.g is deprecated: use g2 instead
from k import *

m.f() # N: m.f is deprecated: use f2 instead
p.s.g() # N: p.s.g is deprecated: use g2 instead
n.f() # N: m.f is deprecated: use f2 instead
ps.g() # N: p.s.g is deprecated: use g2 instead
m.f() # N: function m.f is deprecated: use f2 instead
p.s.g() # N: function p.s.g is deprecated: use g2 instead
n.f() # N: function m.f is deprecated: use f2 instead
ps.g() # N: function p.s.g is deprecated: use g2 instead
f()
g()
h() # N: k.h is deprecated: use h2 instead
h() # N: function k.h is deprecated: use h2 instead

[file m.py]
from typing_extensions import deprecated
Expand Down Expand Up @@ -107,16 +107,16 @@ from typing_extensions import deprecated
@deprecated("use C2 instead")
class C: ...

c: C # N: __main__.C is deprecated: use C2 instead
C() # N: __main__.C is deprecated: use C2 instead
C.missing() # N: __main__.C is deprecated: use C2 instead \
c: C # N: class __main__.C is deprecated: use C2 instead
C() # N: class __main__.C is deprecated: use C2 instead
C.missing() # N: class __main__.C is deprecated: use C2 instead \
# E: "Type[C]" has no attribute "missing"
C.__init__(c) # N: __main__.C is deprecated: use C2 instead
C(1) # N: __main__.C is deprecated: use C2 instead \
C.__init__(c) # N: class __main__.C is deprecated: use C2 instead
C(1) # N: class __main__.C is deprecated: use C2 instead \
# E: Too many arguments for "C"
D = C # N: __main__.C is deprecated: use C2 instead
D = C # N: class __main__.C is deprecated: use C2 instead
D()
t = (C, C, D) # N: __main__.C is deprecated: use C2 instead
t = (C, C, D) # N: class __main__.C is deprecated: use C2 instead

[builtins fixtures/tuple.pyi]

Expand All @@ -127,17 +127,17 @@ import m
import p.s
import m as n
import p.s as ps
from m import C # N: m.C is deprecated: use C2 instead
from p.s import D # N: p.s.D is deprecated: use D2 instead
from m import C # N: class m.C is deprecated: use C2 instead
from p.s import D # N: class p.s.D is deprecated: use D2 instead
from k import *

m.C() # N: m.C is deprecated: use C2 instead
p.s.D() # N: p.s.D is deprecated: use D2 instead
n.C() # N: m.C is deprecated: use C2 instead
ps.D() # N: p.s.D is deprecated: use D2 instead
m.C() # N: class m.C is deprecated: use C2 instead
p.s.D() # N: class p.s.D is deprecated: use D2 instead
n.C() # N: class m.C is deprecated: use C2 instead
ps.D() # N: class p.s.D is deprecated: use D2 instead
C()
D()
E() # N: k.E is deprecated: use E2 instead
E() # N: class k.E is deprecated: use E2 instead

[file m.py]
from typing_extensions import deprecated
Expand Down Expand Up @@ -168,9 +168,9 @@ from typing_extensions import deprecated
class C:
def __init__(self) -> None: ...

c: C # N: __main__.C is deprecated: use C2 instead
C() # N: __main__.C is deprecated: use C2 instead
C.__init__(c) # N: __main__.C is deprecated: use C2 instead
c: C # N: class __main__.C is deprecated: use C2 instead
C() # N: class __main__.C is deprecated: use C2 instead
C.__init__(c) # N: class __main__.C is deprecated: use C2 instead

[builtins fixtures/tuple.pyi]

Expand Down Expand Up @@ -208,14 +208,14 @@ class B:

a = A()
b = B()
a + 1 # N: __main__.A.__add__ is deprecated: no A + int
1 + a # N: __main__.A.__radd__ is deprecated: no int + A
a += 1 # N: __main__.A.__iadd__ is deprecated: no A = A + int
for i in a: # N: __main__.A.__iter__ is deprecated: no iteration
a + 1 # N: function __main__.A.__add__ is deprecated: no A + int
1 + a # N: function __main__.A.__radd__ is deprecated: no int + A
a += 1 # N: function __main__.A.__iadd__ is deprecated: no A = A + int
for i in a: # N: function __main__.A.__iter__ is deprecated: no iteration
reveal_type(i) # N: Revealed type is "builtins.int"
1 in a # N: __main__.A.__iter__ is deprecated: no iteration
1 in b # N: __main__.B.__contains__ is deprecated: no in
~a # N: __main__.A.__invert__ is deprecated: no inversion
1 in a # N: function __main__.A.__iter__ is deprecated: no iteration
1 in b # N: function __main__.B.__contains__ is deprecated: no in
~a # N: function __main__.A.__invert__ is deprecated: no inversion

[builtins fixtures/tuple.pyi]

Expand Down Expand Up @@ -248,12 +248,12 @@ class A:
def __iadd__(self, v: Union[int, str]) -> A: ...

a = A()
a + 1 # N: __main__.A.__add__ is deprecated [overload def (__main__.A, builtins.int)]: no A + int
a + 1 # N: overload def (__main__.A, builtins.int) of function __main__.A.__add__ is deprecated: no A + int
a + "x"
1 + a
"x" + a # N: __main__.A.__radd__ is deprecated [overload def (__main__.A, builtins.str)]: no str + A
a += 1 # N: __main__.A.__iadd__ is deprecated: no A += Any
a += "x" # N: __main__.A.__iadd__ is deprecated: no A += Any
"x" + a # N: overload def (__main__.A, builtins.str) of function __main__.A.__radd__ is deprecated: no str + A
a += 1 # N: function __main__.A.__iadd__ is deprecated: no A += Any
a += "x" # N: function __main__.A.__iadd__ is deprecated: no A += Any

[builtins fixtures/tuple.pyi]

Expand All @@ -276,18 +276,18 @@ class C:
@staticmethod
def k() -> None: ...

C.f # N: __main__.C.f is deprecated: use g instead
C().f # N: __main__.C.f is deprecated: use g instead
C().f() # N: __main__.C.f is deprecated: use g instead
C().f(1) # N: __main__.C.f is deprecated: use g instead \
C.f # N: function __main__.C.f is deprecated: use g instead
C().f # N: function __main__.C.f is deprecated: use g instead
C().f() # N: function __main__.C.f is deprecated: use g instead
C().f(1) # N: function __main__.C.f is deprecated: use g instead \
# E: Too many arguments for "f" of "C"
f = C().f # N: __main__.C.f is deprecated: use g instead
f() # N: __main__.C.f is deprecated: use g instead
t = (C.f, C.f, C.g) # N: __main__.C.f is deprecated: use g instead
f = C().f # N: function __main__.C.f is deprecated: use g instead
f() # N: function __main__.C.f is deprecated: use g instead
t = (C.f, C.f, C.g) # N: function __main__.C.f is deprecated: use g instead

C().g()
C().h() # N: __main__.C.h is deprecated: use g instead
C().k() # N: __main__.C.k is deprecated: use g instead
C().h() # N: function __main__.C.h is deprecated: use g instead
C().k() # N: function __main__.C.k is deprecated: use g instead

[builtins fixtures/callable.pyi]

Expand All @@ -302,9 +302,9 @@ class C:
def f(self) -> None: ...
def g(self) -> None: ...

C().f() # N: __main__.C is deprecated: use D instead \
# N: __main__.C.f is deprecated: use g instead
C().g() # N: __main__.C is deprecated: use D instead
C().f() # N: class __main__.C is deprecated: use D instead \
# N: function __main__.C.f is deprecated: use g instead
C().g() # N: class __main__.C is deprecated: use D instead

[builtins fixtures/callable.pyi]

Expand All @@ -325,17 +325,17 @@ class C:
def g(self, v: int) -> None: ...


C.f # N: __main__.C.f is deprecated: use f2 instead
C().f # N: __main__.C.f is deprecated: use f2 instead
C().f() # N: __main__.C.f is deprecated: use f2 instead \
C.f # N: function __main__.C.f is deprecated: use f2 instead
C().f # N: function __main__.C.f is deprecated: use f2 instead
C().f() # N: function __main__.C.f is deprecated: use f2 instead \
# E: "int" not callable
C().f = 1 # E: Property "f" defined in "C" is read-only \
# N: __main__.C.f is deprecated: use f2 instead
# N: function __main__.C.f is deprecated: use f2 instead

C.g
C().g
C().g = 1 # N: __main__.C.g is deprecated: use g2 instead
C().g = "x" # N: __main__.C.g is deprecated: use g2 instead \
C().g = 1 # N: function __main__.C.g is deprecated: use g2 instead
C().g = "x" # N: function __main__.C.g is deprecated: use g2 instead \
# E: Incompatible types in assignment (expression has type "str", variable has type "int")

[builtins fixtures/property.pyi]
Expand All @@ -353,10 +353,10 @@ def f(x: str) -> str: ...
@deprecated("use f2 instead")
def f(x: Union[int, str]) -> Union[int, str]: ...

f # N: __main__.f is deprecated: use f2 instead
f(1) # N: __main__.f is deprecated: use f2 instead
f("x") # N: __main__.f is deprecated: use f2 instead
f(1.0) # N: __main__.f is deprecated: use f2 instead \
f # N: function __main__.f is deprecated: use f2 instead
f(1) # N: function __main__.f is deprecated: use f2 instead
f("x") # N: function __main__.f is deprecated: use f2 instead
f(1.0) # N: function __main__.f is deprecated: use f2 instead \
# E: No overload variant of "f" matches argument type "float" \
# N: Possible overload variants: \
# N: def f(x: int) -> int \
Expand All @@ -370,7 +370,7 @@ def g(x: str) -> str: ...
def g(x: Union[int, str]) -> Union[int, str]: ...

g
g(1) # N: __main__.g is deprecated [overload def (x: builtins.int) -> builtins.int]: work with str instead
g(1) # N: overload def (x: builtins.int) -> builtins.int of function __main__.g is deprecated: work with str instead
g("x")
g(1.0) # E: No overload variant of "g" matches argument type "float" \
# N: Possible overload variants: \
Expand All @@ -386,7 +386,7 @@ def h(x: Union[int, str]) -> Union[int, str]: ...

h
h(1)
h("x") # N: __main__.h is deprecated [overload def (x: builtins.str) -> builtins.str]: work with int instead
h("x") # N: overload def (x: builtins.str) -> builtins.str of function __main__.h is deprecated: work with int instead
h(1.0) # E: No overload variant of "h" matches argument type "float" \
# N: Possible overload variants: \
# N: def h(x: int) -> int \
Expand Down

0 comments on commit 286371f

Please sign in to comment.