Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use types defined in preceding classes as type annotations #39

Open
robinzyb opened this issue Feb 17, 2023 · 4 comments
Open

cannot use types defined in preceding classes as type annotations #39

robinzyb opened this issue Feb 17, 2023 · 4 comments

Comments

@robinzyb
Copy link

Dear Developers,
I have encountered the following error when I used py2puml.

I was trying to draw uml diagram of the deepmd package with the command,

py2puml deepmd/train deepmd.train

I have the error messages:

Traceback (most recent call last):
  File "/data/ybzhuang/conda/env/dpff/bin/py2puml", line 8, in <module>
    sys.exit(run())
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/cli.py", line 17, in run
    print(''.join(py2puml(args.path, args.module)))
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/py2puml.py", line 12, in py2puml
    inspect_package(
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/inspection/inspectpackage.py", line 20, in inspect_package
    inspect_module(
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/inspection/inspectmodule.py", line 60, in inspect_module
    inspect_domain_definition(definition_type, root_module_name, domain_items_by_fqn, domain_relations)
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/inspection/inspectmodule.py", line 47, in inspect_domain_definition
    inspect_class_type(
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/inspection/inspectclass.py", line 99, in inspect_class_type
    attributes = inspect_static_attributes(
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/inspection/inspectclass.py", line 78, in inspect_static_attributes
    attr_type, full_namespaced_definitions = shorten_compound_type_annotation(attr_raw_type, module_resolver)
  File "/data/ybzhuang/conda/env/dpff/lib/python3.9/site-packages/py2puml/parsing/astvisitors.py", line 215, in shorten_compound_type_annotation
    raise ValueError(f'Could not resolve type {compound_type_part} in module {module_resolver.module}: it needs to be imported explicitely.')
ValueError: Could not resolve type deepmd.train.run_options.HVD in module <module 'deepmd.train.run_options' from '/data/ybzhuang/repositories/deepmd-kit/deepmd/train/run_options.py'>: it needs to be imported explicitely.

How should I bypass this implicit-typing errors?

@lucsorel
Copy link
Owner

hi @robinzyb, thank you for reporting this issue.

This error is due to declaring a static attribute using a forward reference and resolving it later at runtime:

  1. declaration of RunOptions._HVD: Optional["HVD"] (https://github.com/deepmodeling/deepmd-kit/blob/master/deepmd/train/run_options.py#L85)
  2. import attempt in _try_init_distrib(): https://github.com/deepmodeling/deepmd-kit/blob/master/deepmd/train/run_options.py#L184

When py2puml parses the codebase, _try_init_distrib() is not called and "HVD" is not resolved. I cannot do much to solve this unusual way to declare types and import them in a method. The only thing I can do is to add a working mode that won't fail when encountering these cases, but it won't be anytime soon, I am sorry 😞

@robinzyb
Copy link
Author

Hi @lucsorel , thank you for the quick reply! Adding a bypassing mechanism is a good idea. I'm looking forward to it. I have drawn uml diagrams for part of the package, which maybe satisfy my needs now.

@lucsorel lucsorel changed the title needs to be imported explicitely. needs to be imported explicitely. type needs to be imported explicitely. Mar 5, 2023
@Paulo-99
Copy link

Paulo-99 commented Mar 5, 2024

Good afternoon
I had the same error raised when I tried to type something as a class property like this:

from typing import TypedDict

class ObjectA:
  
    class ResponseDict(TypedDict):
         id: int
         name: str

    def __init__(self):
        pass

class ObjectB:
  
    class ResponseDict(TypedDict):
         id: int
         name: str
         date: str

    def __init__(self):
        pass


class Response(TypedDict):
    objects_a: list[ObjectA.ResponseDict]
    object_b: ObjectB.ResponseDict

when I ran py2puml I got the error it needs to be imported explicitly
I changed my code so that ResponseDict were independant classes so I could import them directly but would it be possible for this case to be handled ?

@lucsorel
Copy link
Owner

lucsorel commented Mar 6, 2024

hello @Paulo-99

Thank you fro reporting the issue. "type needs to be imported explicitely" is a generic error message that is raised by py2puml when it fails to "understand" the type of a parameter in a class it inspects (probably, the types of Response.objects_a and Response.object_b). I have very little free time to handle the feature requests and issues of this project, I am sorry. I cannot provide deadlines for these 😞

@lucsorel lucsorel changed the title type needs to be imported explicitely. cannot use types defined in preceding classes as type annotations Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants