Skip to content

Unable to parse special class definitions #2879

@jieguangzhou

Description

@jieguangzhou
from superduper import Component
import typing as t


class MyComponent(Component):
    new_component: t.Optional["MyComponent"] = None


component = MyComponent(identifier="123")
❯ python test.py
/Users/zhouhaha/Work/Superduper/superduper/superduper/base/config_settings.py:106: UserWarning: Warning: The path '/session/secrets' is not a valid directory.
  warn(f"Warning: The path '{secrets_volume}' is not a valid directory.")
Traceback (most recent call last):
  File "/Users/zhouhaha/Work/Superduper/superduper/test.py", line 9, in <module>
    component = MyComponent(identifier="123")
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 7, in __init__
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/components/component.py", line 199, in __post_init__
    self.postinit()
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/components/component.py", line 561, in postinit
    self._original_parameters = self.dict()
                                ^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/components/component.py", line 745, in dict
    r = self._dict(metadata=metadata)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/components/component.py", line 738, in _dict
    return super().dict(metadata=metadata)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/base/base.py", line 395, in dict
    return Document(r, schema=self.class_schema)
                              ^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/annotations.py", line 103, in __get__
    self._cache[owner] = self.func(owner)
                         ^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/base/base.py", line 102, in class_schema
    named_fields = cls._new_fields
                   ^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/annotations.py", line 103, in __get__
    self._cache[owner] = self.func(owner)
                         ^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/base/base.py", line 93, in _new_fields
    s = get_schema(cls)[0]
        ^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/schema.py", line 235, in get_schema
    inferred_cls, iterable = process(annotation)
                             ^^^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/schema.py", line 201, in process
    inferred_cls, iterable_ = _process_union(args)
                              ^^^^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/schema.py", line 109, in _process_union
    return process(args[0])
           ^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/schema.py", line 215, in process
    inferred_cls = _evaluate_forward_ref(
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/Work/Superduper/superduper/superduper/misc/schema.py", line 28, in _evaluate_forward_ref
    return ref._evaluate(globalns, localns or {}, set())  # type: ignore[arg-type]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zhouhaha/.pyenv/versions/3.11.10/lib/python3.11/typing.py", line 905, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'MyComponent' is not defined. Did you mean: 'Component'?


Another case

from superduper import Component
import typing as t


class MyComponent(Component):
    new_component: t.Optional["MyComponent1"] = None


class MyComponent1(Component): ...


component = MyComponent(identifier="123")

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions