Skip to content

Commit

Permalink
Make "deprecated" Note a standard Error, disabled by default (#18192)
Browse files Browse the repository at this point in the history
While working on the relase of mypy 1.14 we noticed a large number of
notes for deprecated. Speaking with Jukka, he suggested we make this
disabled by default. And if it's disabled by default, having it as a
note is not as useful. We also don't have many stand alone notes. Most
notes are "attached" with an error.

This PR makes the deprecated error disabled by default and by default
reports it as error when enabled. `--report-deprecated-as-note` can
be used to report them as notes instead.
  • Loading branch information
svalentin authored Dec 4, 2024
1 parent 242873a commit f51090d
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 186 deletions.
10 changes: 5 additions & 5 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,12 @@ potentially problematic or redundant in some way.

This limitation will be removed in future releases of mypy.

.. option:: --report-deprecated-as-error
.. option:: --report-deprecated-as-note

By default, mypy emits notes if your code imports or uses deprecated
features. This flag converts such notes to errors, causing mypy to
eventually finish with a non-zero exit code. Features are considered
deprecated when decorated with ``warnings.deprecated``.
If error code ``deprecated`` is enabled, mypy emits errors if your code
imports or uses deprecated features. This flag converts such errors to
notes, causing mypy to eventually finish with a zero exit code. Features
are considered deprecated when decorated with ``warnings.deprecated``.

.. _miscellaneous-strictness-flags:

Expand Down
10 changes: 5 additions & 5 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ incorrect control flow or conditional checks that are accidentally always true o
Check that imported or used feature is deprecated [deprecated]
--------------------------------------------------------------

By default, mypy generates a note if your code imports a deprecated feature explicitly with a
If you use :option:`--enable-error-code deprecated <mypy --enable-error-code>`,
mypy generates an error if your code imports a deprecated feature explicitly with a
``from mod import depr`` statement or uses a deprecated feature imported otherwise or defined
locally. Features are considered deprecated when decorated with ``warnings.deprecated``, as
specified in `PEP 702 <https://peps.python.org/pep-0702>`_. You can silence single notes via
``# type: ignore[deprecated]`` or turn off this check completely via ``--disable-error-code=deprecated``.
Use the :option:`--report-deprecated-as-error <mypy --report-deprecated-as-error>` option for
more strictness, which turns all such notes into errors.
specified in `PEP 702 <https://peps.python.org/pep-0702>`_.
Use the :option:`--report-deprecated-as-note <mypy --report-deprecated-as-note>` option to
turn all such errors into notes.

.. note::

Expand Down
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7696,7 +7696,7 @@ def warn_deprecated(self, node: SymbolNode | None, context: Context) -> None:
and ((deprecated := node.deprecated) is not None)
and not self.is_typeshed_stub
):
warn = self.msg.fail if self.options.report_deprecated_as_error else self.msg.note
warn = self.msg.note if self.options.report_deprecated_as_note else self.msg.fail
warn(deprecated, context, code=codes.DEPRECATED)


Expand Down
1 change: 1 addition & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def __hash__(self) -> int:
"deprecated",
"Warn when importing or using deprecated (overloaded) functions, methods or classes",
"General",
default_enabled=False,
)

# This copy will not include any error codes defined later in the plugins.
Expand Down
4 changes: 2 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,10 @@ def add_invertible_flag(
group=lint_group,
)
add_invertible_flag(
"--report-deprecated-as-error",
"--report-deprecated-as-note",
default=False,
strict_flag=False,
help="Report importing or using deprecated features as errors instead of notes",
help="Report importing or using deprecated features as notes instead of errors",
group=lint_group,
)

Expand Down
2 changes: 1 addition & 1 deletion mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __init__(self) -> None:
self.warn_return_any = False

# Report importing or using deprecated features as errors instead of notes.
self.report_deprecated_as_error = False
self.report_deprecated_as_note = False

# Warn about unused '# type: ignore' comments
self.warn_unused_ignores = False
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def check_and_warn_deprecated(self, info: TypeInfo, ctx: Context) -> None:
if isinstance(imp, ImportFrom) and any(info.name == n[0] for n in imp.names):
break
else:
warn = self.fail if self.options.report_deprecated_as_error else self.note
warn = self.note if self.options.report_deprecated_as_note else self.fail
warn(deprecated, ctx, code=codes.DEPRECATED)

def analyze_type_with_type_info(
Expand Down
Loading

0 comments on commit f51090d

Please sign in to comment.