Skip to content

Commit

Permalink
Merge pull request #165 from sco1/type-ignore-opt
Browse files Browse the repository at this point in the history
Release v3.1.0
  • Loading branch information
sco1 committed May 6, 2024
2 parents ef0cfc0 + e698866 commit ec8b88b
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.0.1
current_version = 3.1.0
commit = False

[bumpversion:file:README.md]
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Expand Up @@ -4,7 +4,7 @@ ci:

repos:
- repo: https://github.com/psf/black
rev: 24.3.0
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/pycqa/isort
Expand All @@ -13,7 +13,7 @@ repos:
- id: isort
name: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-merge-conflict
- id: check-toml
Expand All @@ -25,8 +25,9 @@ repos:
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
exclude: "test_type_ignore.py"
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
rev: v0.4.2
hooks:
- id: ruff
- repo: local
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
# Changelog
Versions follow [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (`<major>`.`<minor>`.`<patch>`)

## [v3.1.0]
### Added
* #164 Add `--respect-type-ignore` to support suppression of errors for functions annotated with `type: ignore`

## [v3.0.1]
### Changed
* #155 Remove upper bound on Python constraint
Expand Down
14 changes: 12 additions & 2 deletions README.md
@@ -1,5 +1,5 @@
# flake8-annotations
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flake8-annotations/3.0.1?logo=python&logoColor=FFD43B)](https://pypi.org/project/flake8-annotations/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flake8-annotations/3.1.0?logo=python&logoColor=FFD43B)](https://pypi.org/project/flake8-annotations/)
[![PyPI](https://img.shields.io/pypi/v/flake8-annotations?logo=Python&logoColor=FFD43B)](https://pypi.org/project/flake8-annotations/)
[![PyPI - License](https://img.shields.io/pypi/l/flake8-annotations?color=magenta)](https://github.com/sco1/flake8-annotations/blob/main/LICENSE)
[![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)
Expand Down Expand Up @@ -32,7 +32,7 @@ cog.out(
]]] -->
```bash
$ flake8 --version
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
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
```
<!-- [[[end]]] -->

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

Default: `False`

### `--respect-type-ignore`
Suppress linting errors for functions annotated with a `# type: ignore` comment.

**NOTE:** Type ignore tags are not considered, e.g. `# type: ignore[arg-type]` is treated the same as `# type: ignore`.

Default: `False`


## Generic Functions
Per the Python Glossary, a [generic function](https://docs.python.org/3/glossary.html#term-generic-function) is defined as:

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

## Contributing
### Python Version Support
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/).

### Development Environment
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:
Expand Down
2 changes: 1 addition & 1 deletion flake8_annotations/__init__.py
@@ -1 +1 @@
__version__ = "3.0.1"
__version__ = "3.1.0"
22 changes: 22 additions & 0 deletions flake8_annotations/checker.py
Expand Up @@ -40,13 +40,18 @@ def __init__(self, tree: t.Optional[ast.Module], lines: t.List[str]):

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

# Type ignores are provided by ast at the module level & we'll need them later when deciding
# whether or not to emit errors for a given function
self._type_ignore_lineno = {ti.lineno for ti in self.tree.type_ignores}

# Set by flake8's config parser
self.suppress_none_returning: bool
self.suppress_dummy_args: bool
self.allow_untyped_defs: bool
self.allow_untyped_nested: bool
self.mypy_init_return: bool
self.allow_star_arg_any: bool
self.respect_type_ignore: bool
self.dispatch_decorators: t.Set[str]
self.overload_decorators: t.Set[str]

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

# Optionally respect a type: ignore comment
# These are considered at the function level & tags are not considered
if self.respect_type_ignore and (function.lineno in self._type_ignore_lineno):
continue

# Yield explicit errors for arguments that are missing annotations
for arg in function.get_missed_annotations():
# Check for type comments here since we're not considering them as typed args
Expand Down Expand Up @@ -222,6 +232,17 @@ def add_options(cls, parser: OptionManager) -> None: # pragma: no cover
help="Suppress ANN401 for dynamically typed *args and **kwargs. (Default: %(default)s)",
)

parser.add_option(
"--respect-type-ignore",
default=False,
action="store_true",
parse_from_config=True,
help=(
"Supress errors for functions annotated with a 'type: ignore' comment. (Default: "
"%(default)s)"
),
)

@classmethod
def parse_options(cls, options: Namespace) -> None: # pragma: no cover
"""Parse the custom configuration options given to flake8."""
Expand All @@ -231,6 +252,7 @@ def parse_options(cls, options: Namespace) -> None: # pragma: no cover
cls.allow_untyped_nested = options.allow_untyped_nested
cls.mypy_init_return = options.mypy_init_return
cls.allow_star_arg_any = options.allow_star_arg_any
cls.respect_type_ignore = options.respect_type_ignore

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

0 comments on commit ec8b88b

Please sign in to comment.