Skip to content

Consider disallowing forward references to non-global names in __future__.annotations (for compatibility) #1747

Description

@BHSPitMonkey

Summary

Examples: https://play.ty.dev/e6628610-7be6-4090-82ca-d1cadf92e85f

Beginning with the alpha.28 release (and still happening as of alpha.30), ty emits invalid-type-form for valid types that happen to share a name with an instance method.

Example involving the set builtin:

class Foo:
  def set(self) -> None:
    pass

  def bar(self) -> set[int]:  # invalid-type-form
    return {1, 2, 3}

  def baz(self) -> set:       # invalid-type-form
    return {"hello", "world"}
Invalid subscript of object of type `def set(self) -> None` in type expression
Variable of type `def set(self) -> None` is not allowed in a type expression

Example using a non-builtin type:

from datetime import datetime

class Foo:
  def datetime(self) -> None:
    pass

  def maybe_return_date(self) -> datetime | None:  # invalid-type-form
    return None
Variable of type `def datetime(self) -> None` is not allowed

Version

ty 0.0.1-alpha.28, ty 0.0.1-alpha.29, ty 0.0.1-alpha.30

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.needs-decisionAwaiting a decision from a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions