Skip to content

Conversation

@bzoracler
Copy link
Contributor

@bzoracler bzoracler commented Oct 27, 2025

This is a follow-on to #20104 (do not merge until #20104 has been merged). The relevant commits on top of #20104 are the last 2:


As mentioned in #20104 (comment) it is likely a good idea to report implicit usages of @deprecated() constructors in a callable-like context, e.g.

from collections.abc import Callable
from typing_extensions import deprecated

class A:
    @deprecated("do not use this")
    def __init__(self) -> None: ...

var: Callable[..., A] = A  # E: function A.__init__ is deprecated: do not use this

This PR supports this in the following contexts if the class constructor or one of its overloads is @deprecated() :

  • The type context is Callable, a compatible callback protocol, or a union with at least 1 such item;
  • The context location is an assignment, argument to a callable, or return value from a callable.

cc @sterliakov who initially suggested this expansion.

Gets a class constructor type if it's used in a valid callable type context.
Considers the following cases as valid contexts:
* A plain `Callable` context is always treated as a valid context.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not want to add subtype checks for this case, as this seems like the most common case and at worst the user will get both a "deprecated" report and an "incompatible types in assignment" report.

Comment on lines +5999 to +6000
_valid_pep702_context = self._valid_pep702_type_context
self._valid_pep702_type_context = valid_pep702_type_context
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In 0950c97 the same effect (avoiding false positives in the else branch of a conditional expression) could have also been implemented by doing this instead of having the boolean flag member self._valid_pep702_type_context:

with self.msg.filter_errors(filter_deprecated=not valid_pep702_type_context):
    ...

I chose not to do this because it would just compute the deprecation warnings, which involves subtype checking, and discard it without usage.

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant