[![build status](https://github.com/asottile/flake8-typing-imports/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/flake8-typing-imports/actions/workflows/main.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/flake8-typing-imports/main.svg)](https://results.pre-commit.ci/latest/github/asottile/flake8-typing-imports/main)
flake8-typing-imports
=====================
flake8 plugin which checks that typing imports are properly guarded
## installation
```bash
pip install flake8-typing-imports
```
## flake8 codes
| Code | Description |
|--------|-----------------------------------------------------------------------|
| TYP001 | guard import by `TYPE_CHECKING` |
| TYP002 | `@overload` is broken in <3.5.2 |
| TYP003 | `Union[Match, ...]` or `Union[Pattern, ...]` must be quoted in <3.5.2 |
| TYP004 | `NamedTuple` does not support methods in 3.6.0 |
| TYP005 | `NamedTuple` does not support defaults in 3.6.0 |
| TYP006 | guard `typing` attribute by quoting |
## rationale
unfortunately, the `typing` module has been pretty unstable -- it has seen api
changes in 3.5.0, 3.5.2, 3.5.3, 3.5.4, 3.6.0, 3.6.1, 3.6.2, 3.7.0, and 3.7.2!
depending on your supported version of python, you may need to guard your
imports by `if TYPE_CHECKING:` (3.5.2+) or `if False:` if the things you are
importing aren't available in all the pythons you support.
as it's pretty difficult to keep track of what version things changed and you
can't always test against particular patch versions of python, this plugin
helps you statically check this automatically!
```python
# default / --min-python-version 3.5.0
from typing import Type # TYP001
```
```python
# default / --min-python-version 3.5.0
if False:
from typing import Type # OK!
```
```python
# default / --min-python-version 3.5.0
from typing import overload # TYP002
```
```python
# default / --min-python-version 3.5.0
import sys
from typing import overload # OK!
if sys.version_info < (3, 5, 2):
def overload(f):
return f
```
```python
# default / --min-python-version 3.5.0
def foo(bar: Union[Match, str]) -> None: pass # TYP003
def foo(bar: "Union[Match, str]") -> None: pass # OK!
def foo(bar: Union[Pattern, str]) -> None: pass # TYP003
def foo(bar: "Union[Pattern, str]") -> None: pass # OK!
```
```python
# --min-python-version 3.6.0
class NT(NamedTuple):
x: int = 5 # TYP005
def f(self) -> int: # TYP004
return self.x + 4
```
```python
# --min-python-version 3.7.0
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import OrderedDict # OK!
```
## configuration
this plugin has a single configuration point (beyond those provided by flake8)
which is the `--min-python-version` option.
by default, this option is `3.5.0`. this includes all versions of python
which have the `typing` module present.
you can also set this option in the flake8 configuration if you don't want
to use the commandline:
```ini
[flake8]
min_python_version = 3.6.2
```
if a `>=` is set for `python_requires` in `setup.cfg`, that value will be used:
```ini
# setup.cfg setuptools metadata
[options]
python_requires = >=3.6
```
## as a pre-commit hook
See [pre-commit](https://github.com/pre-commit/pre-commit) for instructions
Sample `.pre-commit-config.yaml`:
```yaml
- repo: https://github.com/pycqa/flake8
rev: 3.7.7
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.12.0]
```
Raw data
{
"_id": null,
"home_page": "https://github.com/asottile/flake8-typing-imports",
"name": "flake8-typing-imports",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Anthony Sottile",
"author_email": "asottile@umich.edu",
"download_url": "https://files.pythonhosted.org/packages/e8/45/0cdbd2d3c7d49ff1476241d2d33b966cfdf9f9306b99b7e832d71b2093a2/flake8_typing_imports-1.16.0.tar.gz",
"platform": null,
"description": "[![build status](https://github.com/asottile/flake8-typing-imports/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/flake8-typing-imports/actions/workflows/main.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/flake8-typing-imports/main.svg)](https://results.pre-commit.ci/latest/github/asottile/flake8-typing-imports/main)\n\nflake8-typing-imports\n=====================\n\nflake8 plugin which checks that typing imports are properly guarded\n\n## installation\n\n```bash\npip install flake8-typing-imports\n```\n\n## flake8 codes\n\n| Code | Description |\n|--------|-----------------------------------------------------------------------|\n| TYP001 | guard import by `TYPE_CHECKING` |\n| TYP002 | `@overload` is broken in <3.5.2 |\n| TYP003 | `Union[Match, ...]` or `Union[Pattern, ...]` must be quoted in <3.5.2 |\n| TYP004 | `NamedTuple` does not support methods in 3.6.0 |\n| TYP005 | `NamedTuple` does not support defaults in 3.6.0 |\n| TYP006 | guard `typing` attribute by quoting |\n\n## rationale\n\nunfortunately, the `typing` module has been pretty unstable -- it has seen api\nchanges in 3.5.0, 3.5.2, 3.5.3, 3.5.4, 3.6.0, 3.6.1, 3.6.2, 3.7.0, and 3.7.2!\n\ndepending on your supported version of python, you may need to guard your\nimports by `if TYPE_CHECKING:` (3.5.2+) or `if False:` if the things you are\nimporting aren't available in all the pythons you support.\n\nas it's pretty difficult to keep track of what version things changed and you\ncan't always test against particular patch versions of python, this plugin\nhelps you statically check this automatically!\n\n```python\n# default / --min-python-version 3.5.0\nfrom typing import Type # TYP001\n```\n\n```python\n# default / --min-python-version 3.5.0\nif False:\n from typing import Type # OK!\n```\n\n```python\n# default / --min-python-version 3.5.0\nfrom typing import overload # TYP002\n```\n\n```python\n# default / --min-python-version 3.5.0\nimport sys\nfrom typing import overload # OK!\nif sys.version_info < (3, 5, 2):\n def overload(f):\n return f\n```\n\n```python\n# default / --min-python-version 3.5.0\ndef foo(bar: Union[Match, str]) -> None: pass # TYP003\ndef foo(bar: \"Union[Match, str]\") -> None: pass # OK!\n\ndef foo(bar: Union[Pattern, str]) -> None: pass # TYP003\ndef foo(bar: \"Union[Pattern, str]\") -> None: pass # OK!\n```\n\n```python\n# --min-python-version 3.6.0\nclass NT(NamedTuple):\n x: int = 5 # TYP005\n\n def f(self) -> int: # TYP004\n return self.x + 4\n```\n\n```python\n# --min-python-version 3.7.0\nfrom typing import TYPE_CHECKING\nif TYPE_CHECKING:\n from typing import OrderedDict # OK!\n```\n\n## configuration\n\nthis plugin has a single configuration point (beyond those provided by flake8)\nwhich is the `--min-python-version` option.\n\nby default, this option is `3.5.0`. this includes all versions of python\nwhich have the `typing` module present.\n\nyou can also set this option in the flake8 configuration if you don't want\nto use the commandline:\n\n```ini\n[flake8]\nmin_python_version = 3.6.2\n```\n\nif a `>=` is set for `python_requires` in `setup.cfg`, that value will be used:\n\n```ini\n# setup.cfg setuptools metadata\n\n[options]\npython_requires = >=3.6\n```\n\n## as a pre-commit hook\n\nSee [pre-commit](https://github.com/pre-commit/pre-commit) for instructions\n\nSample `.pre-commit-config.yaml`:\n\n```yaml\n- repo: https://github.com/pycqa/flake8\n rev: 3.7.7\n hooks:\n - id: flake8\n additional_dependencies: [flake8-typing-imports==1.12.0]\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "flake8 plugin which checks that typing imports are properly guarded",
"version": "1.16.0",
"project_urls": {
"Homepage": "https://github.com/asottile/flake8-typing-imports"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "feb4437c4fc09a752f6fe436d7baf3a13da2117c4e6b94de5e09906b7288a957",
"md5": "09f47b8fcfe2f7355d411fb5f1d25a98",
"sha256": "269ec08f1ea000a6122752f72b17816a7dc19b5b2e7ea808ddceec25ba859bf4"
},
"downloads": -1,
"filename": "flake8_typing_imports-1.16.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "09f47b8fcfe2f7355d411fb5f1d25a98",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.8",
"size": 7796,
"upload_time": "2024-10-11T21:52:57",
"upload_time_iso_8601": "2024-10-11T21:52:57.620580Z",
"url": "https://files.pythonhosted.org/packages/fe/b4/437c4fc09a752f6fe436d7baf3a13da2117c4e6b94de5e09906b7288a957/flake8_typing_imports-1.16.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e8450cdbd2d3c7d49ff1476241d2d33b966cfdf9f9306b99b7e832d71b2093a2",
"md5": "c0b63913770b745a70869e43d07a008a",
"sha256": "06d84504734026bfb914bc8d172e23559784055701f8d8c4880458698e98d3ca"
},
"downloads": -1,
"filename": "flake8_typing_imports-1.16.0.tar.gz",
"has_sig": false,
"md5_digest": "c0b63913770b745a70869e43d07a008a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7449,
"upload_time": "2024-10-11T21:52:59",
"upload_time_iso_8601": "2024-10-11T21:52:59.151934Z",
"url": "https://files.pythonhosted.org/packages/e8/45/0cdbd2d3c7d49ff1476241d2d33b966cfdf9f9306b99b7e832d71b2093a2/flake8_typing_imports-1.16.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-11 21:52:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "asottile",
"github_project": "flake8-typing-imports",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "flake8-typing-imports"
}