# [flake8-modern-annotations](https://github.com/plinss/flake8-modern-annotations)
flake8 plugin to validate type annotations accoring to modern practices.
* Postponed Evaluations of Annotations per PEP 563.
* Standard collection generics per PEP 585.
* Union types as X | Y per PEP 604.
* Optional types when PEP 604 Unions are available.
### Activation
By default the plugin activates according to the Python version used for flake8
or when it sees a future import that enables modern annoations, e.g.:
from __future__ import annotations
Options exist for each feature to override the automatic activation.
## Installation
Standard python package installation:
pip install flake8-modern-annotations
## Type Aliases
Note that there are some restrictions when using modern annotation proactices with type aliases:
* Forward references
* Must use string literals
* Unions containing forward references must use `typing.Union`
* Standard collection generics
* Cannot be used in type aliases if subscripted on Python < 3.9, e.g. `X: TypeAlias = dict[str, str]`
* Unions
* `|` unions cannot be used in type aliases on Python < 3.9
This plugin will not report errors for the above cases with the default settings.
It is recommended to use the `TypeAlias` type for type aliases to help this plugin detect them properly in all cases.
`TypeAlias` is available from `typing` in Python 3.10+ and `typing_extensions` in prior versions.
## Options
`modern-annotations-postponed`
: Controls validation of postponed annotations (PEP 563),
choices: `auto`, `always`, `never` (default: `auto`)
`modern-annotations-deprecated`
: Controls validation of deprecated types (PEP 585),
choices: `auto`, `always`, `never` (default: `auto`)
`modern-annotations-type-alias`
: Use deprecated types in type aliases (required for older Python < 3.9),
choices: `auto`, `always`, `never` (default: `auto`)
`modern-annotations-union`
: Controls checks for use of typing.Union (PEP 604),
choices: `auto`, `always`, `never` (default: `auto`)
`modern-annotations-optional`
: Controls checks for use of typing.Optional,
choices: `auto`, `always`, `never` (default: `auto`)
`modern-annotations-include-name`
: Include plugin name in messages
`modern-annotations-no-include-name`
: Do not include plugin name in messages (default setting)
All options may be specified on the command line with a `--` prefix,
or can be placed in your flake8 config file.
`auto` settings turn on or off depending on the version of Python that flake8 is running on,
and the presence of `from __future__ import annotations` in the code,
which enables the modern annotations in Python 3.7+.
If developing code in Python 3.9+ that is expected to run on 3.7 or 3.8,
use `modern-annotations-type-alias=always` to force older behavior of type aliases
and ensure that the code will work.
## Error Codes
| Code | Message |
|--------|---------|
| MDA001 | Remove quotes from variable type annotation 'type'
| MDA002 | Remove quotes from argument type annotation 'type'
| MDA003 | Remove quotes from return type annotation 'type'
| MDA100 | 'typing.Tuple' is deprecated, remove from import
| MDA101 | 'typing.List' is deprecated, remove from import
| MDA102 | 'typing.Dict' is deprecated, remove from import
| MDA103 | 'typing.Set' is deprecated, remove from import
| MDA104 | 'typing.FrozenSet' is deprecated, remove from import
| MDA105 | 'typing.Type' is deprecated, remove from import
| MDA110 | 'typing.Deque' is deprecated, replace with 'collections.deque'
| MDA111 | 'typing.DefaultDict' is deprecated, replace with 'collections.defaultdict'
| MDA112 | 'typing.OrderedDict' is deprecated, replace with 'collections.OrderedDict'
| MDA113 | 'typing.Counter' is deprecated, replace with 'collections.Counter'
| MDA114 | 'typing.ChainMap' is deprecated, replace with 'collections.ChainMap'
| MDA120 | 'typing.Awaitable' is deprecated, replace with 'collections.abc.Awaitable'
| MDA121 | 'typing.Coroutine' is deprecated, replace with 'collections.abc.Coroutine'
| MDA122 | 'typing.AsyncIterable' is deprecated, replace with 'collections.abc.AsyncIterable'
| MDA123 | 'typing.AsyncIterator' is deprecated, replace with 'collections.abc.AsyncIterator'
| MDA124 | 'typing.AsyncGenerator' is deprecated, replace with 'collections.abc.AsyncGenerator'
| MDA125 | 'typing.Iterable' is deprecated, replace with 'collections.abc.Iterable'
| MDA126 | 'typing.Iterator' is deprecated, replace with 'collections.abc.Iterator'
| MDA127 | 'typing.Generator' is deprecated, replace with 'collections.abc.Generator'
| MDA128 | 'typing.Reversible' is deprecated, replace with 'collections.abc.Reversible'
| MDA129 | 'typing.Container' is deprecated, replace with 'collections.abc.Container'
| MDA130 | 'typing.Collection' is deprecated, replace with 'collections.abc.Collection'
| MDA131 | 'typing.Callable' is deprecated, replace with 'collections.abc.Callable'
| MDA132 | 'typing.AbstractSet' is deprecated, replace with 'collections.abc.Set'
| MDA133 | 'typing.MutableSet' is deprecated, replace with 'collections.abc.MutableSet'
| MDA134 | 'typing.Mapping' is deprecated, replace with 'collections.abc.Mapping'
| MDA135 | 'typing.MutableMapping' is deprecated, replace with 'collections.abc.MutableMapping'
| MDA136 | 'typing.Sequence' is deprecated, replace with 'collections.abc.Sequence'
| MDA137 | 'typing.MutableSequence' is deprecated, replace with 'collections.abc.MutableSequence'
| MDA138 | 'typing.ByteString' is deprecated, replace with 'collections.abc.ByteString'
| MDA139 | 'typing.MappingView' is deprecated, replace with 'collections.abc.MappingView'
| MDA140 | 'typing.KeysView' is deprecated, replace with 'collections.abc.KeysView'
| MDA141 | 'typing.ItemsView' is deprecated, replace with 'collections.abc.ItemsView'
| MDA142 | 'typing.ValuesView' is deprecated, replace with 'collections.abc.ValuesView'
| MDA150 | 'typing.ContextManager' is deprecated, replace with 'contextlib.AbstractContextManager'
| MDA151 | 'typing.AsyncContextManager' is deprecated, replace with 'contextlib.AbstractAsyncContextManager'
| MDA160 | 'typing.Pattern' is deprecated, replace with 're.Pattern'
| MDA161 | 'typing.Match' is deprecated, replace with 're.Match'
| MDA200 | Replace 'Tuple' with 'tuple'
| MDA201 | Replace 'List' with 'list'
| MDA202 | Replace 'Dict' with 'dict'
| MDA203 | Replace 'Set' with 'set'
| MDA204 | Replace 'FrozenSet' with 'frozenset'
| MDA205 | Replace 'Type' with 'type'
| MDA210 | Replace 'Deque' with 'collections.deque'
| MDA211 | Replace 'DefaultDict' with 'collections.defaultdict'
| MDA212 | Replace 'OrderedDict' with 'collections.OrderedDict'
| MDA213 | Replace 'Counter' with 'collections.Counter'
| MDA214 | Replace 'ChainMap' with 'collections.ChainMap'
| MDA220 | Replace 'Awaitable' with 'collections.abc.Awaitable'
| MDA221 | Replace 'Coroutine' with 'collections.abc.Coroutine'
| MDA222 | Replace 'AsyncIterable' with 'collections.abc.AsyncIterable'
| MDA223 | Replace 'AsyncIterator' with 'collections.abc.AsyncIterator'
| MDA224 | Replace 'AsyncGenerator' with 'collections.abc.AsyncGenerator'
| MDA225 | Replace 'Iterable' with 'collections.abc.Iterable'
| MDA226 | Replace 'Iterator' with 'collections.abc.Iterator'
| MDA227 | Replace 'Generator' with 'collections.abc.Generator'
| MDA228 | Replace 'Reversible' with 'collections.abc.Reversible'
| MDA229 | Replace 'Container' with 'collections.abc.Container'
| MDA230 | Replace 'Collection' with 'collections.abc.Collection'
| MDA231 | Replace 'Callable' with 'collections.abc.Callable'
| MDA232 | Replace 'AbstractSet' with 'collections.abc.Set'
| MDA233 | Replace 'MutableSet' with 'collections.abc.MutableSet'
| MDA234 | Replace 'Mapping' with 'collections.abc.Mapping'
| MDA235 | Replace 'MutableMapping' with 'collections.abc.MutableMapping'
| MDA236 | Replace 'Sequence' with 'collections.abc.Sequence'
| MDA237 | Replace 'MutableSequence' with 'collections.abc.MutableSequence'
| MDA238 | Replace 'ByteString' with 'collections.abc.ByteString'
| MDA239 | Replace 'MappingView' with 'collections.abc.MappingView'
| MDA240 | Replace 'KeysView' with 'collections.abc.KeysView'
| MDA241 | Replace 'ItemsView' with 'collections.abc.ItemsView'
| MDA242 | Replace 'ValuesView' with 'collections.abc.ValuesView'
| MDA250 | Replace 'ContextManager' with 'contextlib.AbstractContextManager'
| MDA251 | Replace 'AsyncContextManager' with 'contextlib.AbstractAsyncContextManager'
| MDA260 | Replace 'Pattern' with 're.Pattern'
| MDA261 | Replace 'Match' with 're.Match'
| MDA400 | 'typing.Union' is deprecated, remove from import
| MDA401 | Replace 'Union' with '|'
| MDA500 | 'typing.Optional' is deprecated, remove from import
| MDA501 | Replace 'Optional' with '| None'
## Examples
```
x: 'Foo' <-- MDA001
def foo(x: 'Foo') -> None: <-- MDA002
def foo(x: Foo) -> 'Bar': <-- MDA003
from typing import Dict <-- MDA102
x: Dict[str, str] <-- MDA202
from typing import Dict
MyDict = Dict[str, int] <-- no error on Python 3.7/3.8
from typing import Union <-- MDA400
x: Union[int, float] <-- MDA401
from typing import Optional <-- MDA500
x: Optional[int] <-- MDA501
```
Raw data
{
"_id": null,
"home_page": "",
"name": "flake8-modern-annotations",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "flake8,noqa",
"author": "",
"author_email": "Peter Linss <pypi@linss.com>",
"download_url": "https://files.pythonhosted.org/packages/04/d2/cab70d7f8e1dc05706797f1de801c8035992d4a987718e785e9768791c1f/flake8-modern-annotations-1.6.0.tar.gz",
"platform": null,
"description": "# [flake8-modern-annotations](https://github.com/plinss/flake8-modern-annotations)\n\nflake8 plugin to validate type annotations accoring to modern practices.\n\n* Postponed Evaluations of Annotations per PEP 563.\n* Standard collection generics per PEP 585.\n* Union types as X | Y per PEP 604.\n* Optional types when PEP 604 Unions are available.\n\n### Activation\n\nBy default the plugin activates according to the Python version used for flake8 \nor when it sees a future import that enables modern annoations, e.g.:\n\n from __future__ import annotations\n\nOptions exist for each feature to override the automatic activation.\n\n## Installation\n\nStandard python package installation:\n\n pip install flake8-modern-annotations\n\n\n## Type Aliases\n\nNote that there are some restrictions when using modern annotation proactices with type aliases:\n\n* Forward references\n * Must use string literals\n * Unions containing forward references must use `typing.Union`\n\n* Standard collection generics\n * Cannot be used in type aliases if subscripted on Python < 3.9, e.g. `X: TypeAlias = dict[str, str]`\n\n* Unions\n * `|` unions cannot be used in type aliases on Python < 3.9\n\nThis plugin will not report errors for the above cases with the default settings.\n\nIt is recommended to use the `TypeAlias` type for type aliases to help this plugin detect them properly in all cases.\n`TypeAlias` is available from `typing` in Python 3.10+ and `typing_extensions` in prior versions.\n\n## Options\n\n`modern-annotations-postponed`\n: Controls validation of postponed annotations (PEP 563), \nchoices: `auto`, `always`, `never` (default: `auto`)\n\n`modern-annotations-deprecated`\n: Controls validation of deprecated types (PEP 585), \nchoices: `auto`, `always`, `never` (default: `auto`)\n\n`modern-annotations-type-alias`\n: Use deprecated types in type aliases (required for older Python < 3.9), \nchoices: `auto`, `always`, `never` (default: `auto`)\n\n`modern-annotations-union`\n: Controls checks for use of typing.Union (PEP 604), \nchoices: `auto`, `always`, `never` (default: `auto`)\n\n`modern-annotations-optional`\n: Controls checks for use of typing.Optional, \nchoices: `auto`, `always`, `never` (default: `auto`)\n\n`modern-annotations-include-name`\n: Include plugin name in messages\n\n`modern-annotations-no-include-name`\n: Do not include plugin name in messages (default setting)\n\nAll options may be specified on the command line with a `--` prefix,\nor can be placed in your flake8 config file.\n\n`auto` settings turn on or off depending on the version of Python that flake8 is running on,\nand the presence of `from __future__ import annotations` in the code, \nwhich enables the modern annotations in Python 3.7+.\n\nIf developing code in Python 3.9+ that is expected to run on 3.7 or 3.8,\nuse `modern-annotations-type-alias=always` to force older behavior of type aliases\nand ensure that the code will work.\n\n\n## Error Codes\n\n| Code | Message |\n|--------|---------|\n| MDA001 | Remove quotes from variable type annotation 'type'\n| MDA002 | Remove quotes from argument type annotation 'type'\n| MDA003 | Remove quotes from return type annotation 'type'\n| MDA100 | 'typing.Tuple' is deprecated, remove from import\n| MDA101 | 'typing.List' is deprecated, remove from import\n| MDA102 | 'typing.Dict' is deprecated, remove from import\n| MDA103 | 'typing.Set' is deprecated, remove from import\n| MDA104 | 'typing.FrozenSet' is deprecated, remove from import\n| MDA105 | 'typing.Type' is deprecated, remove from import\n| MDA110 | 'typing.Deque' is deprecated, replace with 'collections.deque'\n| MDA111 | 'typing.DefaultDict' is deprecated, replace with 'collections.defaultdict'\n| MDA112 | 'typing.OrderedDict' is deprecated, replace with 'collections.OrderedDict'\n| MDA113 | 'typing.Counter' is deprecated, replace with 'collections.Counter'\n| MDA114 | 'typing.ChainMap' is deprecated, replace with 'collections.ChainMap'\n| MDA120 | 'typing.Awaitable' is deprecated, replace with 'collections.abc.Awaitable'\n| MDA121 | 'typing.Coroutine' is deprecated, replace with 'collections.abc.Coroutine'\n| MDA122 | 'typing.AsyncIterable' is deprecated, replace with 'collections.abc.AsyncIterable'\n| MDA123 | 'typing.AsyncIterator' is deprecated, replace with 'collections.abc.AsyncIterator'\n| MDA124 | 'typing.AsyncGenerator' is deprecated, replace with 'collections.abc.AsyncGenerator'\n| MDA125 | 'typing.Iterable' is deprecated, replace with 'collections.abc.Iterable'\n| MDA126 | 'typing.Iterator' is deprecated, replace with 'collections.abc.Iterator'\n| MDA127 | 'typing.Generator' is deprecated, replace with 'collections.abc.Generator'\n| MDA128 | 'typing.Reversible' is deprecated, replace with 'collections.abc.Reversible'\n| MDA129 | 'typing.Container' is deprecated, replace with 'collections.abc.Container'\n| MDA130 | 'typing.Collection' is deprecated, replace with 'collections.abc.Collection'\n| MDA131 | 'typing.Callable' is deprecated, replace with 'collections.abc.Callable'\n| MDA132 | 'typing.AbstractSet' is deprecated, replace with 'collections.abc.Set'\n| MDA133 | 'typing.MutableSet' is deprecated, replace with 'collections.abc.MutableSet'\n| MDA134 | 'typing.Mapping' is deprecated, replace with 'collections.abc.Mapping'\n| MDA135 | 'typing.MutableMapping' is deprecated, replace with 'collections.abc.MutableMapping'\n| MDA136 | 'typing.Sequence' is deprecated, replace with 'collections.abc.Sequence'\n| MDA137 | 'typing.MutableSequence' is deprecated, replace with 'collections.abc.MutableSequence'\n| MDA138 | 'typing.ByteString' is deprecated, replace with 'collections.abc.ByteString'\n| MDA139 | 'typing.MappingView' is deprecated, replace with 'collections.abc.MappingView'\n| MDA140 | 'typing.KeysView' is deprecated, replace with 'collections.abc.KeysView'\n| MDA141 | 'typing.ItemsView' is deprecated, replace with 'collections.abc.ItemsView'\n| MDA142 | 'typing.ValuesView' is deprecated, replace with 'collections.abc.ValuesView'\n| MDA150 | 'typing.ContextManager' is deprecated, replace with 'contextlib.AbstractContextManager'\n| MDA151 | 'typing.AsyncContextManager' is deprecated, replace with 'contextlib.AbstractAsyncContextManager'\n| MDA160 | 'typing.Pattern' is deprecated, replace with 're.Pattern'\n| MDA161 | 'typing.Match' is deprecated, replace with 're.Match'\n| MDA200 | Replace 'Tuple' with 'tuple'\n| MDA201 | Replace 'List' with 'list'\n| MDA202 | Replace 'Dict' with 'dict'\n| MDA203 | Replace 'Set' with 'set'\n| MDA204 | Replace 'FrozenSet' with 'frozenset'\n| MDA205 | Replace 'Type' with 'type'\n| MDA210 | Replace 'Deque' with 'collections.deque'\n| MDA211 | Replace 'DefaultDict' with 'collections.defaultdict'\n| MDA212 | Replace 'OrderedDict' with 'collections.OrderedDict'\n| MDA213 | Replace 'Counter' with 'collections.Counter'\n| MDA214 | Replace 'ChainMap' with 'collections.ChainMap'\n| MDA220 | Replace 'Awaitable' with 'collections.abc.Awaitable'\n| MDA221 | Replace 'Coroutine' with 'collections.abc.Coroutine'\n| MDA222 | Replace 'AsyncIterable' with 'collections.abc.AsyncIterable'\n| MDA223 | Replace 'AsyncIterator' with 'collections.abc.AsyncIterator'\n| MDA224 | Replace 'AsyncGenerator' with 'collections.abc.AsyncGenerator'\n| MDA225 | Replace 'Iterable' with 'collections.abc.Iterable'\n| MDA226 | Replace 'Iterator' with 'collections.abc.Iterator'\n| MDA227 | Replace 'Generator' with 'collections.abc.Generator'\n| MDA228 | Replace 'Reversible' with 'collections.abc.Reversible'\n| MDA229 | Replace 'Container' with 'collections.abc.Container'\n| MDA230 | Replace 'Collection' with 'collections.abc.Collection'\n| MDA231 | Replace 'Callable' with 'collections.abc.Callable'\n| MDA232 | Replace 'AbstractSet' with 'collections.abc.Set'\n| MDA233 | Replace 'MutableSet' with 'collections.abc.MutableSet'\n| MDA234 | Replace 'Mapping' with 'collections.abc.Mapping'\n| MDA235 | Replace 'MutableMapping' with 'collections.abc.MutableMapping'\n| MDA236 | Replace 'Sequence' with 'collections.abc.Sequence'\n| MDA237 | Replace 'MutableSequence' with 'collections.abc.MutableSequence'\n| MDA238 | Replace 'ByteString' with 'collections.abc.ByteString'\n| MDA239 | Replace 'MappingView' with 'collections.abc.MappingView'\n| MDA240 | Replace 'KeysView' with 'collections.abc.KeysView'\n| MDA241 | Replace 'ItemsView' with 'collections.abc.ItemsView'\n| MDA242 | Replace 'ValuesView' with 'collections.abc.ValuesView'\n| MDA250 | Replace 'ContextManager' with 'contextlib.AbstractContextManager'\n| MDA251 | Replace 'AsyncContextManager' with 'contextlib.AbstractAsyncContextManager'\n| MDA260 | Replace 'Pattern' with 're.Pattern'\n| MDA261 | Replace 'Match' with 're.Match'\n| MDA400 | 'typing.Union' is deprecated, remove from import\n| MDA401 | Replace 'Union' with '|'\n| MDA500 | 'typing.Optional' is deprecated, remove from import\n| MDA501 | Replace 'Optional' with '| None'\n\n\n## Examples\n\n```\nx: 'Foo' <-- MDA001\ndef foo(x: 'Foo') -> None: <-- MDA002\ndef foo(x: Foo) -> 'Bar': <-- MDA003\n\nfrom typing import Dict <-- MDA102\nx: Dict[str, str] <-- MDA202\n\nfrom typing import Dict\nMyDict = Dict[str, int] <-- no error on Python 3.7/3.8\n\nfrom typing import Union <-- MDA400\nx: Union[int, float] <-- MDA401\n\nfrom typing import Optional <-- MDA500\nx: Optional[int] <-- MDA501\n```\n",
"bugtrack_url": null,
"license": "GNU Lesser General Public License v3",
"summary": "Flake8 modern annotations validation",
"version": "1.6.0",
"project_urls": {
"homepage": "https://github.com/plinss/flake8-modern-annotations"
},
"split_keywords": [
"flake8",
"noqa"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4ba3537a6593818b195a886f7e6a136cae33cdde8e8b62428fb5dc63117736e9",
"md5": "126a7da4337eb898baab051d0c19b173",
"sha256": "40342ddf684f12a8882719913adb94a8d4bb4e83e217ec7fe5b03df18acd0ecf"
},
"downloads": -1,
"filename": "flake8_modern_annotations-1.6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "126a7da4337eb898baab051d0c19b173",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 11163,
"upload_time": "2024-01-06T03:38:06",
"upload_time_iso_8601": "2024-01-06T03:38:06.502505Z",
"url": "https://files.pythonhosted.org/packages/4b/a3/537a6593818b195a886f7e6a136cae33cdde8e8b62428fb5dc63117736e9/flake8_modern_annotations-1.6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "04d2cab70d7f8e1dc05706797f1de801c8035992d4a987718e785e9768791c1f",
"md5": "839df54ab2b9c4f15de0864795c5af1c",
"sha256": "879a370b1ba51a7b86bbba1b48393640a12565538b56fc0994630e74f89d9343"
},
"downloads": -1,
"filename": "flake8-modern-annotations-1.6.0.tar.gz",
"has_sig": false,
"md5_digest": "839df54ab2b9c4f15de0864795c5af1c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 16490,
"upload_time": "2024-01-06T03:38:08",
"upload_time_iso_8601": "2024-01-06T03:38:08.275465Z",
"url": "https://files.pythonhosted.org/packages/04/d2/cab70d7f8e1dc05706797f1de801c8035992d4a987718e785e9768791c1f/flake8-modern-annotations-1.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-06 03:38:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "plinss",
"github_project": "flake8-modern-annotations",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "flake8-modern-annotations"
}