flake8-typing-imports


Nameflake8-typing-imports JSON
Version 1.15.0 PyPI version JSON
download
home_pagehttps://github.com/asottile/flake8-typing-imports
Summaryflake8 plugin which checks that typing imports are properly guarded
upload_time2023-10-02 22:26:47
maintainer
docs_urlNone
authorAnthony Sottile
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![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": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Anthony Sottile",
    "author_email": "asottile@umich.edu",
    "download_url": "https://files.pythonhosted.org/packages/78/8c/3cbcbeaa6b9664888b309998123049030fb6a6143242bd8d0554ba3daa72/flake8_typing_imports-1.15.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.15.0",
    "project_urls": {
        "Homepage": "https://github.com/asottile/flake8-typing-imports"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "25bbfad59bf06b5dd6f8b96571839ba7ac95b694b8322d18cdee5cff8dbaf5a4",
                "md5": "502dda7157371cbae2f7286d2430da07",
                "sha256": "4594830f9ecfbe9c75f73de32db3db45d427e837df42bfc478bee6584a2400ec"
            },
            "downloads": -1,
            "filename": "flake8_typing_imports-1.15.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "502dda7157371cbae2f7286d2430da07",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.8",
            "size": 7641,
            "upload_time": "2023-10-02T22:26:45",
            "upload_time_iso_8601": "2023-10-02T22:26:45.141068Z",
            "url": "https://files.pythonhosted.org/packages/25/bb/fad59bf06b5dd6f8b96571839ba7ac95b694b8322d18cdee5cff8dbaf5a4/flake8_typing_imports-1.15.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "788c3cbcbeaa6b9664888b309998123049030fb6a6143242bd8d0554ba3daa72",
                "md5": "f77062867f42b3ccb7daa9605931b6ff",
                "sha256": "6c04b1e9986874d81399f115f5ec683325675268e9b4924e8464f8b1b6b211d1"
            },
            "downloads": -1,
            "filename": "flake8_typing_imports-1.15.0.tar.gz",
            "has_sig": false,
            "md5_digest": "f77062867f42b3ccb7daa9605931b6ff",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 7261,
            "upload_time": "2023-10-02T22:26:47",
            "upload_time_iso_8601": "2023-10-02T22:26:47.064419Z",
            "url": "https://files.pythonhosted.org/packages/78/8c/3cbcbeaa6b9664888b309998123049030fb6a6143242bd8d0554ba3daa72/flake8_typing_imports-1.15.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-02 22:26:47",
    "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"
}
        
Elapsed time: 0.12228s