Skip to content

Quoted forward references incorrectly resolve to shadowing class members #4266

Description

@TyceHerrman

Describe the Bug

A quoted forward reference in a class body resolves against a same-named class
member. The Python typing conformance test expects the quoted reference to
resolve outside the class scope, while the equivalent unquoted annotation
resolves to the class member and is rejected as an invalid type.

Minimal reproduction:

from typing import assert_type


class ClassD:
    def int(self) -> None:
        ...

    x: "int" = 0  # OK: quoted reference resolves to builtins.int
    y: int = 0  # E: local method `int` isn't a legal type expression


assert_type(ClassD.x, int)

The quoted annotation produces an error for x:

Expected a type form, got instance of `(self: Self@ClassD) -> None`

It then treats ClassD.x as Unknown, producing a second failure:

assert_type(Unknown, int) failed

Expected behavior:

  • Accept the quoted annotation "int" and resolve it to builtins.int.
  • Continue rejecting the unquoted annotation int, which refers to the local
    method and isn't a valid type expression.
  • Preserve ClassD.x as int without the cascading Unknown error.

The mismatch appears in
conformance.result.
The corresponding test is
annotations_forward_refs.py.

Related Work

  • Issue #1900 discusses
    same-named methods shadowing types, but its reproduction uses unquoted
    annotations. Its discussion points to the expected error for the unquoted
    case in this conformance test.
  • PR #1983 added an
    overload-specific lookup exception.
  • Issue #357 and
    PR #3617 address unquoted
    forward references evaluated before Python 3.14.
  • Issue #3387 and
    PR #3391 address a different
    from __future__ import annotations case.

This issue specifically covers lookup semantics for a quoted annotation when a
class member shadows the referenced name.

Sandbox Link

Not provided; the reproduction above is self-contained and comes directly from
the typing conformance suite.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

conformanceneeds-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.staletyping-specstuff that might need a spec change

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions