Fix callbacks registered twice when app/app.py runs as a script - #4023
jamalkamaladdin wants to merge 1 commit into
Conversation
Running `python app/app.py` from the parent directory gives the import name `app.app`, but only `app/` is on sys.path, so find_spec imported the parent `app` from the running script and executed it a second time. Every page callback was then registered twice. Check the top-level name first and skip the alias when it does not resolve to a package. Fixes plotly#4011.
|
T4rk1n
left a comment
There was a problem hiding this comment.
Fix is good. Checking only the top-level name with find_spec doesn't import anything, so the running script can no longer be pulled in as its own parent. I confirmed python app/app.py now executes once, and pkg/app.py under uvicorn reload still aliases with and without an __init__.py. It also fixes a case the PR doesn't mention: python app/main.py with a separate app/app.py next to it used to execute that app.py as the parent package. One small ask on the test below; not blocking.
| SAME_NAME = "dash_test_alias_same" | ||
|
|
||
|
|
||
| def test_no_reexecution_when_script_dir_shares_script_name(tmp_path, monkeypatch): |
There was a problem hiding this comment.
Can you add a case for python app/main.py where app/app.py sits next to it? Before this change, find_spec("app.main") imported that other app.py as the parent package, so any Dash app or callbacks in it got registered too. The fix handles it, but nothing guards it. It's worth a line in the CHANGELOG entry too, since that entry only describes the same-name case.



Fixes #4011.
dash/_utils.py:
alias_main_modulereturns early when the first part of a dotted import name is not a package.tests/unit/test_main_module_alias.py: new case for a script inside a folder with its own name.
CHANGELOG.md: new Fixed entry for #4011.
Contributor Checklist
alias_main_modulefor a dotted name whose first part is not a packagepython app/app.pyoptionals
CHANGELOG.md