attrs icon indicating copy to clipboard operation
attrs copied to clipboard

`typing.get_type_hints` cannot resolve forward references in `cls.__init__` annotations

Open mwchase opened this issue 3 years ago • 1 comments

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.

mwchase avatar Sep 05 '22 03:09 mwchase

I'm not sure what to do about this – is it fixable yet at all?

hynek avatar Sep 16 '22 07:09 hynek