#๐Ÿ”’ Circular Imports for Type Hinting

41 messages ยท Page 1 of 1 (latest)

vernal steppe
#

Hi, how would I fix this circular import problem? I could remove the type hints, but I'd rather not.

from agentshell.prompts import TextPrompt

class Shell:
    ...
    def prompt(self, text: str) -> TextPrompt:
        return TextPrompt(self, text)
from agentshell import Shell

class Prompt:
    shell: Shell

    def __init__(self, shell: Shell):
        self.shell = shell
gentle galleonBOT
#

@vernal steppe

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

proper trail
#

conditional import, if typing.TYPE_CHECKING: ...

vernal steppe
#

Is that the only way?

proper trail
#

it's the standard way

vernal steppe
#

I know deferred and all that is an option, but I'm wondering if there's a way to cleanly restructure my code

#

Alright

#

Will do that then, ty

distant torrent
#

shell: "Shell"

proper trail
#

that won't fix the imports

random cove
# vernal steppe Hi, how would I fix this circular import problem? I could remove the type hints,...

you can prevent imports that happen solely for typing hinting by using typing.TYPE_CHECKING

from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
  from agentshell.prompts import TextPrompt

class Shell:
    ...
    def prompt(self, text: str) -> TextPrompt:
        return TextPrompt(self, text)
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
  from agentshell import Shell

class Prompt:
    shell: Shell

    def __init__(self, shell: Shell):
        self.shell = shell
#

and youll need the from __future__ import annotations to convert all type hints to string at runtime

distant torrent
proper trail
#

how would they be resolved then?

distant torrent
#

Remove the "import Shell" line entirely and just use "Shell"?

proper trail
#

how would the static type checker resolve the Shell name to an actual type then?

distant torrent
random cove
proper trail
#

that still requires the future import

random cove
#

no?

proper trail
#

yes

#

because otherwise it will try to resolve the annotation to a name that hasn't been defined yet due to the circular imports

random cove
#

damn

vernal steppe
#

Another question since this is a small one, pyright: ignore[reportUnusedVariable] doesn't seem to work for

    def run(self, prompt: T) -> Any:
        _ = prompt # ignore reportUnusedVariable
        raise NotImplementedError
#

Any tips?

#

On the function sign

#

signature

#

So if I write def run(...) -> Any: # pyright: ignore[reportUnusedVariable]

proper trail
#

what is the point of that line in the first place?

vernal steppe
#

The function?

proper trail
#

the _ = prompt

vernal steppe
#

As a stop-gap measure that does the same thing

proper trail
#

as what?

vernal steppe
#

To ignore the unused variable

proper trail
#

oh, ignores an unused parameter

#

I didn't know pyright would complain about that

#

seems unwarranted

#

but this has nothing to do with annotations anymore; maybe open a new question about the pyright warning?

modern turret
#

Another way is to do a module import. ```py
from future import annotations

import module

def foo() -> module.MyType:...

gentle galleonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.