Skip to content

typing.get_type_hints cannot resolve forward references in cls.__init__ annotations #1021

Open
@mwchase

Description

@mwchase

The is related to #593/#760. Because #760 populates the generated __init__ method's globals at the time that the class is created, new globals aren't visible to get_type_hints by default.

Example code demonstrating this:

import attrs
import typing

@attrs.define
class HasForwardRef:
    ref: "ClassDefinedLater"

class ClassDefinedLater:
    pass

typing.get_type_hints(HasForwardRef.__init__)

This last call raises an error.

I've got a workaround for this in the code where I hit this, but it really wasn't obvious at first what was going on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypingTyping/stub/Mypy/PyRight related bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions