os._Environ.get() and __contains__() raise and catch KeyError internally
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
Since os._Environ is a subclass of collections.abc.Mapping, its get() and __contains__() methods are implemented in terms of __getitem__(). By forwarding to __getitem__(), a KeyError is raised when the key does not exist. This KeyError is then immediately caught and swallowed.
Handling this exception is extra, avoidable work. _Environ can check its internal dictionary directly which is more efficient.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-156492
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the os._Environ implementations of get() and contains() described in the issue, and inspect how missing keys are currently handled. The change is complete when those methods avoid raising and immediately catching KeyError while preserving their existing results for missing keys.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100