Skip to content

Commit 8b341ba

Browse files
committed
Add back --report-deprecated-as-note to change error to note
1 parent d5fb882 commit 8b341ba

File tree

8 files changed

+39
-7
lines changed

8 files changed

+39
-7
lines changed

docs/source/command_line.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ potentially problematic or redundant in some way.
537537

538538
This limitation will be removed in future releases of mypy.
539539

540+
.. option:: --report-deprecated-as-note
541+
542+
If error code ``deprecated`` is enabled, mypy emits errors if your code
543+
imports or uses deprecated features. This flag converts such errors to
544+
notes, causing mypy to eventually finish with a zero exit code. Features
545+
are considered deprecated when decorated with ``warnings.deprecated``.
540546

541547
.. _miscellaneous-strictness-flags:
542548

docs/source/error_code_list2.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@ If you use :option:`--enable-error-code deprecated <mypy --enable-error-code>`,
240240
mypy generates an error if your code imports a deprecated feature explicitly with a
241241
``from mod import depr`` statement or uses a deprecated feature imported otherwise or defined
242242
locally. Features are considered deprecated when decorated with ``warnings.deprecated``, as
243-
specified in `PEP 702 <https://peps.python.org/pep-0702>`_. You can silence single errors via
244-
``# type: ignore[deprecated]`` or turn off this check completely via ``--disable-error-code=deprecated``.
243+
specified in `PEP 702 <https://peps.python.org/pep-0702>`_.
244+
Use the :option:`--report-deprecated-as-note <mypy --report-deprecated-as-note>` option to
245+
turn all such errors into notes.
245246

246247
.. note::
247248

mypy/checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7696,8 +7696,8 @@ def warn_deprecated(self, node: SymbolNode | None, context: Context) -> None:
76967696
and ((deprecated := node.deprecated) is not None)
76977697
and not self.is_typeshed_stub
76987698
):
7699-
self.msg.fail(deprecated, context, code=codes.DEPRECATED)
7700-
7699+
warn = self.msg.note if self.options.report_deprecated_as_note else self.msg.fail
7700+
warn(deprecated, context, code=codes.DEPRECATED)
77017701

77027702
class CollectArgTypeVarTypes(TypeTraverserVisitor):
77037703
"""Collects the non-nested argument types in a set."""

mypy/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# Show error codes for some note-level messages (these usually appear alone
2222
# and not as a comment for a previous error-level message).
23-
SHOW_NOTE_CODES: Final = {codes.ANNOTATION_UNCHECKED}
23+
SHOW_NOTE_CODES: Final = {codes.ANNOTATION_UNCHECKED, codes.DEPRECATED}
2424

2525
# Do not add notes with links to error code docs to errors with these codes.
2626
# We can tweak this set as we get more experience about what is helpful and what is not.

mypy/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,13 @@ def add_invertible_flag(
804804
help="Warn about statements or expressions inferred to be unreachable",
805805
group=lint_group,
806806
)
807+
add_invertible_flag(
808+
"--report-deprecated-as-note",
809+
default=False,
810+
strict_flag=False,
811+
help="Report importing or using deprecated features as notes instead of errors",
812+
group=lint_group,
813+
)
807814

808815
# Note: this group is intentionally added here even though we don't add
809816
# --strict to this group near the end.

mypy/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ def __init__(self) -> None:
173173
# declared with a precise type
174174
self.warn_return_any = False
175175

176+
# Report importing or using deprecated features as errors instead of notes.
177+
self.report_deprecated_as_note = False
178+
176179
# Warn about unused '# type: ignore' comments
177180
self.warn_unused_ignores = False
178181

mypy/typeanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ def check_and_warn_deprecated(self, info: TypeInfo, ctx: Context) -> None:
790790
if isinstance(imp, ImportFrom) and any(info.name == n[0] for n in imp.names):
791791
break
792792
else:
793-
self.fail(deprecated, ctx, code=codes.DEPRECATED)
793+
warn = self.note if self.options.report_deprecated_as_note else self.fail
794+
warn(deprecated, ctx, code=codes.DEPRECATED)
794795

795796
def analyze_type_with_type_info(
796797
self, info: TypeInfo, args: Sequence[Type], ctx: Context, empty_tuple_index: bool

test-data/unit/check-deprecated.test

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Type checker test cases for reporting deprecations.
22

33

4-
[case testDeprecatedDisableNotes]
4+
[case testDeprecatedDisabled]
55

66
from typing_extensions import deprecated
77

@@ -13,6 +13,20 @@ f()
1313
[builtins fixtures/tuple.pyi]
1414

1515

16+
[case testDeprecatedAsNoteWithErrorCode]
17+
# flags: --enable-error-code=deprecated --show-error-codes --report-deprecated-as-note
18+
19+
from typing_extensions import deprecated
20+
21+
@deprecated("use f2 instead")
22+
def f() -> None: ...
23+
24+
f() # type: ignore[deprecated]
25+
f() # N: function __main__.f is deprecated: use f2 instead [deprecated]
26+
27+
[builtins fixtures/tuple.pyi]
28+
29+
1630
[case testDeprecatedAsErrorWithErrorCode]
1731
# flags: --enable-error-code=deprecated --show-error-codes
1832

0 commit comments

Comments
 (0)