这是indexloc提供的服务,不要输入任何密码
Skip to content

[E402] Add exception for warnings.filterwarnings() before imports #21423

@tsvikas

Description

@tsvikas

E402 currently allows sys.path and os.environ modifications before imports. I'd like to propose adding warnings.filterwarnings() (and related warning module functions) to this exception list.

Rationale:

  • It's a common pattern to filter warnings before importing modules that emit warnings during import
  • Like sys.path modifications, warning filters affect import-time behavior
  • Currently requires verbose # noqa: E402 comments on every subsequent import

Example:

import warnings

warnings.filterwarnings("ignore", category=DeprecationWarning)

import some_module  # Currently triggers E402

Proposed: Treat warnings.filterwarnings(), warnings.simplefilter(), and warnings.resetwarnings() the same as sys.path modifications.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionAwaiting a decision from a maintainerruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions