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

Suggest importing from typing_extensions #14591

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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