Skip to content
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

Make "deprecated" Note a standard Error, disabled by default #18192

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,6 @@ potentially problematic or redundant in some way.

This limitation will be removed in future releases of mypy.

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

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``.

.. _miscellaneous-strictness-flags:

Expand Down
7 changes: 3 additions & 4 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,12 @@ 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
specified in `PEP 702 <https://peps.python.org/pep-0702>`_. You can silence single errors via
``# type: ignore[deprecated]`` or turn off this check completely via ``--disable-error-code=deprecated``.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "or turn off this check..." part of the sentence could also be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! If we decide to go through with this change, I'll take another look at this.

Use the :option:`--report-deprecated-as-error <mypy --report-deprecated-as-error>` option for
more strictness, which turns all such notes into errors.

.. note::

Expand Down
3 changes: 1 addition & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7696,8 +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(deprecated, context, code=codes.DEPRECATED)
self.msg.fail(deprecated, context, code=codes.DEPRECATED)


class CollectArgTypeVarTypes(TypeTraverserVisitor):
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
2 changes: 1 addition & 1 deletion mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

# Do not add notes with links to error code docs to errors with these codes.
# We can tweak this set as we get more experience about what is helpful and what is not.
Expand Down
7 changes: 0 additions & 7 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,13 +804,6 @@ def add_invertible_flag(
help="Warn about statements or expressions inferred to be unreachable",
group=lint_group,
)
add_invertible_flag(
"--report-deprecated-as-error",
default=False,
strict_flag=False,
help="Report importing or using deprecated features as errors instead of notes",
group=lint_group,
)

# Note: this group is intentionally added here even though we don't add
# --strict to this group near the end.
Expand Down
3 changes: 0 additions & 3 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ def __init__(self) -> None:
# declared with a precise type
self.warn_return_any = False

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

# Warn about unused '# type: ignore' comments
self.warn_unused_ignores = False

Expand Down
3 changes: 1 addition & 2 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +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(deprecated, ctx, code=codes.DEPRECATED)
self.fail(deprecated, ctx, code=codes.DEPRECATED)

def analyze_type_with_type_info(
self, info: TypeInfo, args: Sequence[Type], ctx: Context, empty_tuple_index: bool
Expand Down
Loading