Avoiding cyclic imports, but how...?

Open
#503 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
28/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, python
Domain
compilers

Research direction

Start by reproducing the recursion error from the minimal mylib example using test.py and test.html, then inspect the generated JavaScript around the imports in mylib/a.py, mylib/b.py, mylib/examine.py, and mylib/init.py. Compare the Transcrypt output with CPython's behavior and determine whether cyclic imports can be supported or need a documented limitation and workaround.

Written by the indexing model from the issue text.

Description

Hi, I'm still trying to port a library that initially was written to run with PyJS to Transcrypt, and got into one more issue where I was not able to find any running solution. I am trying to describe the problem in a much simpler test case, which does exactly the same and runs in the same problem, a "too much recursion" message in the browser.

.
├── mylib
│   ├── a.py
│   ├── b.py
│   ├── examine.py
│   └── __init__.py
├── test.html
└── test.py

mylib/a.py:

from mylib import examine

class A(object):
        def examine(self):
                examine.examine()

mylib/b.py:

from mylib.a import A

class B(A):
        pass

mylib/examine.py:

import mylib

def examine():
        for key in dir(mylib):
                print(key)

mylib/__init__.py:

from mylib.a import A
from mylib.b import B

import mylib.examine

The reason for the function examine() in mylib.examine is, that it both should be callable outise of A or in context of A, and it examines the contents of the mylib module, its objects and classes.

CPython compiles and runs this without compromise. The Transcrypt-generated output runs into an recursion error, because mylib.examine imports examine. According to issue #106, cyclic imports are a problem. So, surely, this is more a question how I have to style my code, but I can't find a solution right now... can this problem be solved in any way?

Dominant language
Python
Stars
2.9k
Forks
218
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TranscryptOrg/Transcrypt

All issues in TranscryptOrg/Transcrypt

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.