Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Proposal: Enforce generator on contextlib.contextmanager decorated functions #11686

Open
max-muoto opened this issue Jun 2, 2024 · 0 comments
Labels
rule Implementing or modifying a lint rule

Comments

@max-muoto
Copy link
Contributor

max-muoto commented Jun 2, 2024

In the past, it was fairly common for people to simply put Iterator on context managers simply out of convenience, as typing out Generator, required adding in two (mostly useless) generic params. However, this can lead to runtime failures not caught by a type-checker, take this example from python/typeshed#2772.

import contextlib
from typing import Iterator

@contextlib.contextmanager
def f() -> Iterator[int]:
    return iter([1])


with f():
    pass


with f():
    raise TypeError('wat')

Due to how breaking this change is, the typeshed maintainers were are reluctant to change the underlying annotation to require a Generator. It was actually proposed offloading this responsibility to linting tules, or special rules within the type-checkers themselves.

Now, Iterator[int] can be simplified to Generator[int] with the addition of type var defaults, and their usage in the cpython implementation, and the typesheds, see: python/typeshed#11867

I would propose adding a rule to autofix Iterator[T] to Generator[T] on 3.13+ for any functions decorated with contextlib.contextmanager, or when deferred annotations are present in the file, as otherwise a runtime error will be encountered. In cases where neither are true, an error can simply be flagged and fixed manually (either through quoting Generator[T], or doing Generator[T, None, None]).

This will help ensure that type-checkers such as Pyright and MyPy can flag and catch issues that might be hidden as a result.

@max-muoto max-muoto changed the title Rule Proposal: Enforce generator on contextlib.contextmanager decorated function Rule Proposal: Enforce generator on contextlib.contextmanager decorated functions Jun 2, 2024
@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

2 participants