Skip to content

Commit

Permalink
Suggest importing from typing_extensions (#14591)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Feb 2, 2023
1 parent e2e0fbe commit 0929773
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,16 @@ def report_missing_module_attribute(
):
# Yes. Generate a helpful note.
self.msg.add_fixture_note(fullname, context)
else:
typing_extensions = self.modules.get("typing_extensions")
if typing_extensions and source_id in typing_extensions.names:
self.msg.note(
f"Use `from typing_extensions import {source_id}` instead", context
)
self.msg.note(
"See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module",
context,
)

def process_import_over_existing_name(
self,
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -2198,3 +2198,17 @@ def foo(x: int) -> Union[Generator[A, None, None], Generator[B, None, None]]:
[case testNoCrashOnStarRightHandSide]
x = *(1, 2, 3) # E: Can use starred expression only as assignment target
[builtins fixtures/tuple.pyi]


[case testTypingExtensionsSuggestion]
from typing import _FutureFeatureFixture

# This import is only needed in tests. In real life, mypy will always have typing_extensions in its
# build due to its pervasive use in typeshed. This assumption may one day prove False, but when
# that day comes this suggestion will also be less helpful than it is today.
import typing_extensions
[out]
main:1: error: Module "typing" has no attribute "_FutureFeatureFixture"
main:1: note: Use `from typing_extensions import _FutureFeatureFixture` instead
main:1: note: See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module
[builtins fixtures/tuple.pyi]
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ def TypedDict(typename: str, fields: Dict[str, Type[_T]], *, total: Any = ...) -
def reveal_type(__obj: T) -> T: pass

def dataclass_transform() -> Callable[[T], T]: ...

_FutureFeatureFixture = 0

0 comments on commit 0929773

Please sign in to comment.