Tool: SonarLint in PyCharm
- SontarLint 22.214.171.124615
- PyCharm 2023.1.1
- Python 3.10
Why I believe it’s a false-positive:
When I originally made myself this context manager, I tried to research how it should be hinted.
The internet came up with 3 ideas:
- Generator - what SonarLint suggests I should do,
- Iterator - what SonarLint previously didn’t recognise but was fixed after being reported as false-positive,
- and typing.ContextManager / contextlib.AbstractContextManager - the source suggested the former but typing.ContextManager is deprecated since 3.9/PEP585 and contextlib.AbstractContextManager is its official full substitution
I’m using the last one because it the best conveys what I’m doing - a context manager, duh. Being on 3.10 I use the contextlib.AbstractContextManager but typing.ContextManager also gets marked.
This code predates my use of SonarLint, I just had to do something in that file and noticed the big yellow marks on my code - and that’s how I ended up here, actually reporting it.
import contextlib @contextlib.contextmanager def test() -> contextlib.AbstractContextManager[str]: yield "this is an example"