Skip to content

(Closes #3461) duplicated script names - #3468

Open
hiker wants to merge 15 commits into
masterfrom
3461_duplicated_script_names
Open

(Closes #3461) duplicated script names#3468
hiker wants to merge 15 commits into
masterfrom
3461_duplicated_script_names

Conversation

@hiker

@hiker hiker commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

Fixes #3461.

@hiker

hiker commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator Author

I've fixed the unit test failures by slightly changing the test (instead of setting sys.path, I chdir into the script directory).

We do get additional failure in example, since without modifying sys.path, helper scripts in the same directory as the psyclone scripts are NOT found anymore (unless you set PYTHONPATH=.). We could still modify sys.path in generator.py to accommodate that use case, I have started the CI to see if nemo/lfric relies on this.

…orting helper scripts, but clean up sys.pathin the end.
@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a74dfdf) to head (fdee332).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #3468   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          397       397           
  Lines        55577     55569    -8     
=========================================
- Hits         55577     55569    -8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hiker

hiker commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator Author

Failures in one example and nemo (lfric is fine) indicate that we should modify sys.path when importing and executing a script (so that helper scripts in the same directory as the script are found). That was done in ab86dd3, so once I've updated the documentation this should be ready to go.

@LonelyCat124 LonelyCat124 changed the title 3461 duplicated script names (Closes #3461) duplicated script names Jun 29, 2026
@hiker
hiker temporarily deployed to integration July 6, 2026 04:30 — with GitHub Actions Inactive
@hiker
hiker temporarily deployed to integration July 8, 2026 02:16 — with GitHub Actions Inactive
@hiker

hiker commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

IT passed, ready for review.

@arporter arporter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much @hiker, this feels like a significant improvement. While reviewing it, I got very uncomfortable that our load_script routine (already) modifies global state and that this then has to be manually undone in multiple places after the script(s) have been used. I'm wondering whether there's a better way to do this that will prevent us making mistakes in the clean-up.

Comment thread doc/user_guide/user_scripts.rst Outdated
Comment thread doc/user_guide/user_scripts.rst Outdated
Comment thread src/psyclone/tests/generator_test.py Outdated
@pytest.fixture(name="script_factory", scope="function")
def create_script_factor(tmpdir):
''' Fixture that creates a psyclone optimisation script given the string
def script_factory(tmpdir, code: str) -> Path:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're here, upgrade to tmp_path?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and throughout this file?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All done, I've replaced all of os.path, and using pathlib everywhere now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks :-)

Comment thread src/psyclone/generator.py Outdated
Comment thread src/psyclone/generator.py
Comment thread src/psyclone/generator.py Outdated
@hiker

hiker commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator Author

@arporter , I agree with the refactoring (including using a with). I was originally thinking of doing this, but that would have made this change (which changing the rather essential feature of importing the PSyclone script ;) ) much larger, and potentially more difficult to debug if something should break (e.g. in LFRic).

But if #1629 is being worked on, that would create huge conflicts, since this refactoring will need to introduce additional functions, i.e. moving code around quite a bit to avoid even deeper nesting.

I see three options:

  1. I fix everything else you mentioned, and open a new ticket to refactor this properly. Once Use algorithm PSyIR when generating PSy-layer code #1629 is done, I'll fully implement this. Ideal if Use algorithm PSyIR when generating PSy-layer code #1629 is close to ready and has already refactored code in generator.py.
  2. I think I can add a with statement without much refactoring, but nesting levels might become deep (and the code even harder to understand). This might have only a small impact on Use algorithm PSyIR when generating PSy-layer code #1629 - the deep nesting is mostly in the code_transformation_mode function, so this should not affect LFRic work at all.
  3. I do it properly, and @sergisiso needs to sort it out 👿 Not ideal, that would be quite messy and I don't like it. But if not a lot of work in Use algorithm PSyIR when generating PSy-layer code #1629 has been done on generator.py (but elsewhere so far), that might(!) work.

Can you let me know what you prefer?

@sergisiso

Copy link
Copy Markdown
Collaborator

@hiker My PR won't be ready anytime soon, I found some blockers, but once completed it will simplify the generate.py.

Andy and me discussed this and we decided you can keep the current implementation with the "del" statements, but add a new issue/TODOs, as we want to come back to clean this up at some point.

@hiker

hiker commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator Author

Ready for next review, IT triggered (issue #3514 for cleanup refactoring added).

@hiker

hiker commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

Documentation failure is caused by the NEMO wiki now requiring an account. IT triggered

@arporter arporter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Joerg. ITs were all green. Link failure (as you say) is not your fault and I've opened #3539 for that.
I just have one small comment on one of the test changes and then this is good to go.

config_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"test_files", "dummy_config.cfg")

config_path = Path(get_base_path("lfric")) / ".." / "dummy_config.cfg"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path(get_base_path("lfric")) is just LFRIC_BASE_PATH?

Comment thread src/psyclone/tests/generator_test.py Outdated
@pytest.fixture(name="script_factory", scope="function")
def create_script_factor(tmpdir):
''' Fixture that creates a psyclone optimisation script given the string
def script_factory(tmpdir, code: str) -> Path:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks :-)

Comment thread src/psyclone/generator.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import scripts with the same name more than once (and sys.path growing)

3 participants