typing.ContextManager --- warned against but unactionable?
See original GitHub issueHi @leycec , sorry to bother you again.
I am one to treat warnings as errors, and thus PEP 484/585 require me to make migrations of the form
from typing import Hashable
to
from collections.abc import Hashable
However, there appears not to be an analogue of typing.ContextManager
. Accordingly, what do I do in this situation?
Here’s a minimal example:
from contextlib import contextmanager
from collections.abc import Iterator
from typing import ContextManager
from warnings import filterwarnings, simplefilter
from beartype import beartype
filterwarnings("error")
@contextmanager
@beartype
def foo() -> Iterator[None]:
yield
f = foo()
print("\n\n\nfoo is correctly typed: {}\n\n\n".format(f))
@beartype
def bar(cm: ContextManager[None]) -> None:
pass
bar(f)
print("did not get here")
resulting in:
> python foo.py
foo is correctly typed: <contextlib._GeneratorContextManager object at 0x7f3104e4d3a0>
Traceback (most recent call last):
<snip>
warn(
beartype.roar.BeartypeDecorHintPep585DeprecationWarning: PEP 484 type hint typing.ContextManag
er[NoneType] deprecated by PEP 585 scheduled for removal in the first Python version released
after October 5th, 2025. To resolve this, either drop Python < 3.9 support and globally replac
e this hint by the equivalent PEP 585 type hint (e.g., "typing.List[int]" by "list[int]") or s
ee this discussion topic for saner and more portable solutions:
https://github.com/beartype/beartype#pep-484-deprecations
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Python type hints and context managers - mypy - Stack Overflow
PEP-585 mentions that typing.ContextManager is deprecated in favor of contextlib.AbstractContextManager . But it does not say it's the right ...
Read more >Unable to use typing.ContextManager as return type #476
Describe the bug I am unable to use typing.ContextManager as the return type of a function decorated with contextlib.contextmanager.
Read more >Python Type Hints - How to Type a Context Manager
Python's context manager protocol has only two methods, with straightforward types. But when it comes to adding accurate type hints to a ...
Read more >contextlib — Utilities for with-statement contexts ... - Python Docs
This module provides utilities for common tasks involving the with statement. For more information see also Context Manager Types and With Statement Context ......
Read more >Create launch-node.py · 0465f83c48 - windmill-ops - OpenDev: Free ...
There is a fair bit of assumptions here, but this is a good first start. ... This unactionable warning does not need to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ahh, thanks guys for the help. I could’ve sworn I did a poor man’s
grep
(a.k.a.,Ctrl-F
) forContextManager
, but somehow that went wrong. In any case, onward with the@beartype
ing!…sniped by Spence. Remind me never to foolishly engage in a vicious eBay bidding war over Portal memorabilia with you, Spence. That is the day I would surely lose everything.