#๐Ÿ”’ Circular import

6 messages ยท Page 1 of 1 (latest)

bitter verge
#

I am implement a virtual file system
encounter circular import problem:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .directory import Directory
    from .archive import Archive

class VFile:
  def __init__(self, data: bytes, parent: Directory | Archive ...):
    ...
class Archive(VFile):
  def _read(self):
    for m in metadata:
      file_data = ...
      FileType = guess_type(file_data)
      self.files.append(FileType(data, self))
    ...
from .util import guess_type
class Directory(VFile):
  def _parse_btree(self):
    FileType = guess_type(data)
    self.files.append(FileType(data, self))
    ...

from .common import VFile
from .archive import Archive
from .directory import Directory

def guess_type(data: bytes) -> type[VFile]:
  if is_archive(data):
    return Archive
  if is_dir(data):
    return Directory
  ... 
  return VFile

how to solve

bright mapleBOT
#

@bitter verge

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.

hollow thorn
#

Use a function-local import for guess_type

#

in _parse_btree()

bright mapleBOT
#
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.