Skip to content

Import names from typing directly rather than importing module - #13761

Merged
srittau merged 3 commits into
python:mainfrom
Avasam:Import-names-from-typing-directly-rather-than-importing-module
Apr 12, 2025
Merged

Import names from typing directly rather than importing module#13761
srittau merged 3 commits into
python:mainfrom
Avasam:Import-names-from-typing-directly-rather-than-importing-module

Conversation

@Avasam

@Avasam Avasam commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

Import names from typing and typing_extensions directly rather than importing module. (except where aliasing Self as _Self or TypeAlias as _TypeAlias would cause issues for flake8-pyi's Y015 and Y034)
And avoid aliasing said modules.

This was prompted by this comment: #11204 (comment) where I realized we had some inconsistencies.

Comment thread stdlib/@tests/test_cases/check_re.py Outdated
import mmap
import re
import typing as t
from typing import AnyStr, Match

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm assuming that importing Match from typing rather than re here is intended for the test.

Comment thread pyproject.toml
Comment on lines +219 to +223
[tool.ruff.lint.flake8-import-conventions.aliases]
# Prevent aliasing these, as it causes false-negatives for certain rules
typing_extensions = "typing_extensions"
typing = "typing"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This prevents import typing as t

Comment on lines +8 to +16
def convert(self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None) -> t.Any: ...
def convert(self, value: str, param: click.Parameter | None, ctx: click.Context | None) -> str: ...

class PasswordParamType(click.ParamType):
def convert(self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None) -> t.Any: ...
def convert(self, value: _T, param: click.Parameter | None, ctx: click.Context | None) -> _T: ...

class TextAreaParamType(click.ParamType):
def convert(self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None) -> t.Any: ...
def convert(self, value: _T, param: click.Parameter | None, ctx: click.Context | None) -> _T: ...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@pyhedgehog since you added this stub in #13208
Was there a specific reason to have these as Any?

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit a045be8 into python:main Apr 12, 2025
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
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.

2 participants