Skip to content

os._Environ.get() and __contains__() raise and catch KeyError internally #156491

Description

@alexmalyshev

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-refactorCode refactoring (with no changes in behavior)
    type-featureA feature request or enhancement
    and removed
    type-featureA feature request or enhancement
    type-refactorCode refactoring (with no changes in behavior)
    on Aug 27, 2026
  2. added a commit that references this issue on Sep 6, 2026
  3. added a commit that references this issue on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions