Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PYI034: Inconsistent handling of forward-reference strings for __new__ and __enter__ #22795

@vertti

Description

@vertti

Summary

PYI034 correctly detects -> "ClassName" forward-reference strings for __iadd__ but misses them for __new__ and __enter__.

Reproduction

class Quoted:
    def __new__(cls) -> "Quoted": ...       # NOT caught (should be)
    def __enter__(self) -> "Quoted": ...    # NOT caught (should be)
    def __iadd__(self, other) -> "Quoted": ...  # Caught ✓

class Unquoted:
    def __new__(cls) -> Unquoted: ...       # Caught ✓
    def __enter__(self) -> Unquoted: ...    # Caught ✓
    def __iadd__(self, other) -> Unquoted: ...  # Caught ✓

Running ruff check --select=PYI034 produces:

PYI034 `__iadd__` methods in classes like `Quoted` usually return `self` at runtime
PYI034 `__new__` methods in classes like `Unquoted` usually return `self` at runtime
PYI034 `__enter__` methods in classes like `Unquoted` usually return `self` at runtime
PYI034 `__iadd__` methods in classes like `Unquoted` usually return `self` at runtime
Method -> "Quoted" -> Unquoted
__new__ ❌ missed ✅ caught
__enter__ ❌ missed ✅ caught
__iadd__ ✅ caught ✅ caught

Expected behavior

All 6 methods should trigger PYI034, regardless of whether the return type uses a forward-reference string or direct reference.

Ruff version

ruff 0.9.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing 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