Skip to content

Getting type hints in Field.__set_name__ can cause RuntimeError #73

Closed

Description

@micuda

Hi,
at first, thank You for the great library. =)

For code

from __future__ import annotations

from pyfields import field

class Foo:
  bar: Foo = field()

I got RuntimeError: Error calling __set_name__ on 'NativeField' instance 'bar' in 'Foo'. It is because class Foo does not exist yet and field calls typing.get_type_hints at line 299.

def __set_name__(self,
owner, # type: Type[Any]
name # type: str
):
if owner is not None:
# fill all the information about how it is attached to the class
cls_type_hints = get_type_hints(owner)
self.set_as_cls_member(owner, name, owner_cls_type_hints=cls_type_hints)

For now I just don't annotate attributes like this, but it is weird (and inconsistent) that some attributes have typing annotation and some don't. =)

I'm not sure where and how to fix it.

Activity

  1. changed the title [-]Getting type hints in Field.__set_name__ can cause RuntimeError[/-] [+]Getting type hints in `Field.__set_name__` can cause `RuntimeError`[/+] on Sep 17, 2020
  2. smarie commented on Sep 18, 2020

    @smarie
    Owner

    Thanks @micuda for reporting this issue ! I'll have a look asap

  3. smarie commented on Sep 21, 2020

    @smarie
    Owner

    So after investigating a bit: from __future__ import annotations is a special line introduced in PEP563 in order to enable python 3.10 behaviour in earlier versions. However this behaviour becomes similar to what happens when you use string type hints. So I am able to reproduce the issue without the PEP563:

    from pyfields import field
    
    class Foo:
      bar: 'Foo' = field()

    I will fix this by enabling delayed type hint resolution.

  4. smarie commented on Sep 21, 2020

    @smarie
    Owner

    Fixed in 1.3.0 (as well as field access from class #12, which was waiting a fix on pycharm side that is now available - I had to make the patch for them :) )

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions