Skip to content

Commit ec8b88b

Browse files
authored
Merge pull request #165 from sco1/type-ignore-opt
Release v3.1.0
2 parents ef0cfc0 + e698866 commit ec8b88b

File tree

10 files changed

+237
-177
lines changed

10 files changed

+237
-177
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 3.0.1
2+
current_version = 3.1.0
33
commit = False
44

55
[bumpversion:file:README.md]

.pre-commit-config.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ci:
44

55
repos:
66
- repo: https://github.com/psf/black
7-
rev: 24.3.0
7+
rev: 24.4.2
88
hooks:
99
- id: black
1010
- repo: https://github.com/pycqa/isort
@@ -13,7 +13,7 @@ repos:
1313
- id: isort
1414
name: isort
1515
- repo: https://github.com/pre-commit/pre-commit-hooks
16-
rev: v4.5.0
16+
rev: v4.6.0
1717
hooks:
1818
- id: check-merge-conflict
1919
- id: check-toml
@@ -25,8 +25,9 @@ repos:
2525
hooks:
2626
- id: python-check-blanket-noqa
2727
- id: python-check-blanket-type-ignore
28+
exclude: "test_type_ignore.py"
2829
- repo: https://github.com/astral-sh/ruff-pre-commit
29-
rev: v0.3.4
30+
rev: v0.4.2
3031
hooks:
3132
- id: ruff
3233
- repo: local

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
Versions follow [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (`<major>`.`<minor>`.`<patch>`)
33

4+
## [v3.1.0]
5+
### Added
6+
* #164 Add `--respect-type-ignore` to support suppression of errors for functions annotated with `type: ignore`
7+
48
## [v3.0.1]
59
### Changed
610
* #155 Remove upper bound on Python constraint

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# flake8-annotations
2-
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flake8-annotations/3.0.1?logo=python&logoColor=FFD43B)](https://pypi.org/project/flake8-annotations/)
2+
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flake8-annotations/3.1.0?logo=python&logoColor=FFD43B)](https://pypi.org/project/flake8-annotations/)
33
[![PyPI](https://img.shields.io/pypi/v/flake8-annotations?logo=Python&logoColor=FFD43B)](https://pypi.org/project/flake8-annotations/)
44
[![PyPI - License](https://img.shields.io/pypi/l/flake8-annotations?color=magenta)](https://github.com/sco1/flake8-annotations/blob/main/LICENSE)
55
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/sco1/flake8-annotations/main.svg)](https://results.pre-commit.ci/latest/github/sco1/flake8-annotations/main)
@@ -32,7 +32,7 @@ cog.out(
3232
]]] -->
3333
```bash
3434
$ flake8 --version
35-
6.1.0 (flake8-annotations: 3.0.1, mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.1.0) CPython 3.12.0 on Darwin
35+
7.0.0 (flake8-annotations: 3.1.0, mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.2.0) CPython 3.12.3 on Darwin
3636
```
3737
<!-- [[[end]]] -->
3838

@@ -140,6 +140,14 @@ Suppress `ANN401` for dynamically typed `*args` and `**kwargs`.
140140

141141
Default: `False`
142142

143+
### `--respect-type-ignore`
144+
Suppress linting errors for functions annotated with a `# type: ignore` comment.
145+
146+
**NOTE:** Type ignore tags are not considered, e.g. `# type: ignore[arg-type]` is treated the same as `# type: ignore`.
147+
148+
Default: `False`
149+
150+
143151
## Generic Functions
144152
Per the Python Glossary, a [generic function](https://docs.python.org/3/glossary.html#term-generic-function) is defined as:
145153

@@ -200,6 +208,8 @@ Support is only provided for the following patterns:
200208
Nested dynamic types (e.g. `typing.Tuple[typing.Any]`) and redefinition (e.g. `from typing import Any as Foo`) will not be identified.
201209

202210
## Contributing
211+
### Python Version Support
212+
A best attempt is made to support Python versions until they reach EOL, after which support will be formally dropped by the next minor or major release of this package, whichever arrives first. The status of Python versions can be found [here](https://devguide.python.org/versions/).
203213

204214
### Development Environment
205215
This project uses [Poetry](https://python-poetry.org/) to manage dependencies. With your fork cloned to your local machine, you can install the project and its dependencies to create a development environment using:

flake8_annotations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.1"
1+
__version__ = "3.1.0"

flake8_annotations/checker.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ def __init__(self, tree: t.Optional[ast.Module], lines: t.List[str]):
4040

4141
self.tree = ast.parse("".join(lines), type_comments=True) # flake8 doesn't strip newlines
4242

43+
# Type ignores are provided by ast at the module level & we'll need them later when deciding
44+
# whether or not to emit errors for a given function
45+
self._type_ignore_lineno = {ti.lineno for ti in self.tree.type_ignores}
46+
4347
# Set by flake8's config parser
4448
self.suppress_none_returning: bool
4549
self.suppress_dummy_args: bool
4650
self.allow_untyped_defs: bool
4751
self.allow_untyped_nested: bool
4852
self.mypy_init_return: bool
4953
self.allow_star_arg_any: bool
54+
self.respect_type_ignore: bool
5055
self.dispatch_decorators: t.Set[str]
5156
self.overload_decorators: t.Set[str]
5257

@@ -107,6 +112,11 @@ def run(self) -> t.Generator[FORMATTED_ERROR, None, None]:
107112
if function.has_decorator(self.overload_decorators):
108113
last_overload_decorated_function_name = function.name
109114

115+
# Optionally respect a type: ignore comment
116+
# These are considered at the function level & tags are not considered
117+
if self.respect_type_ignore and (function.lineno in self._type_ignore_lineno):
118+
continue
119+
110120
# Yield explicit errors for arguments that are missing annotations
111121
for arg in function.get_missed_annotations():
112122
# Check for type comments here since we're not considering them as typed args
@@ -222,6 +232,17 @@ def add_options(cls, parser: OptionManager) -> None: # pragma: no cover
222232
help="Suppress ANN401 for dynamically typed *args and **kwargs. (Default: %(default)s)",
223233
)
224234

235+
parser.add_option(
236+
"--respect-type-ignore",
237+
default=False,
238+
action="store_true",
239+
parse_from_config=True,
240+
help=(
241+
"Supress errors for functions annotated with a 'type: ignore' comment. (Default: "
242+
"%(default)s)"
243+
),
244+
)
245+
225246
@classmethod
226247
def parse_options(cls, options: Namespace) -> None: # pragma: no cover
227248
"""Parse the custom configuration options given to flake8."""
@@ -231,6 +252,7 @@ def parse_options(cls, options: Namespace) -> None: # pragma: no cover
231252
cls.allow_untyped_nested = options.allow_untyped_nested
232253
cls.mypy_init_return = options.mypy_init_return
233254
cls.allow_star_arg_any = options.allow_star_arg_any
255+
cls.respect_type_ignore = options.respect_type_ignore
234256

235257
# Store decorator lists as sets for easier lookup
236258
cls.dispatch_decorators = set(options.dispatch_decorators)

0 commit comments

Comments
 (0)