Skip to content

Conversation

@AlexWaygood
Copy link
Member

Summary

I was looking into #13246 to see if we could land the proposed change as part of Ruff 0.7, and noticed that we're currently pretty inconsistent in how we try to determine whether something is a Python source file or not (and, if so, what kind of source file it is). This PR tries to make it so that we only ever use ruff_python_ast::PySourceType for that purpose. This removes duplicated logic and the number of magic "py" or "pyi" strings across the codebase, and it should make it easier to make changes such as #13246 in the future.

Test Plan

cargo test

@AlexWaygood AlexWaygood added the internal An internal refactor or improvement label Oct 8, 2024
@AlexWaygood AlexWaygood force-pushed the alex/remove-redundant-routines branch from bd426ea to 6251505 Compare October 8, 2024 17:45
Comment on lines 75 to 76
} else if Path::new(url.path())
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case("ipynb"))
{
} else if PySourceType::from(url.path()).is_ipynb() {
Copy link
Member Author

Choose a reason for hiding this comment

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

this compares case-sensitively where the existing code compares case-insensitively. I don't actually know if that's correct for Jupyter notebooks, but I do know that casing does matter for other Python files. import foo succeeds if the foo module is found at foo.py, but fails if the foo module is found at foo.PY

Copy link
Member

Choose a reason for hiding this comment

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

Hard to get data on this...

Screenshot 2024-10-08 at 1 24 01 PM

Copy link
Member

Choose a reason for hiding this comment

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

Seems overkill to make the breaking change for ipynb, I think the .PY import logic does make sense though?

Copy link
Member

Choose a reason for hiding this comment

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

this compares case-sensitively where the existing code compares case-insensitively. I don't actually know if that's correct for Jupyter notebooks, but I do know that casing does matter for other Python files. import foo succeeds if the foo module is found at foo.py, but fails if the foo module is found at foo.PY

Is this also true on case insensitive file systems? I do think that import resolver is different from what we consider a python file

Copy link
Member

@MichaReiser MichaReiser Oct 8, 2024

Choose a reason for hiding this comment

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

My experience is that most applications don't consider file extensions to be case sensitive. Naming a file test.txt or test.TXT doesn't change the application in which my desktop environment opens the file. That's why I think we shouldn't treat file extensions as case sensitive but I can see how this is beyond the scope of this pr

It does make sense that the module resolver only tests for the existence of ’.py’ and whether ’.PY’ is considered to match depends on the file system's case sensitivity

Copy link
Member Author

Choose a reason for hiding this comment

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

For now I will revert these changes so that this PR is a "pure refactor" that does not change functionality. I think we may need to review this, though. In most places, we currently do case-sensitive matching for file extensions. Maybe that's correct or maybe it's incorrect, but it seems like we're pretty inconsistent right now. (And if that inconsistency is correct, because we need to apply logic in different situations, then we could at least add some comments to explain it more clearly 😄)

@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@AlexWaygood AlexWaygood requested review from zanieb and removed request for carljm October 9, 2024 10:49
@MichaReiser
Copy link
Member

Could you create an issue to re-visit the case-insensitive extension matching and assign it to the next milestone. We should re visit this soon.

@AlexWaygood AlexWaygood enabled auto-merge (squash) October 9, 2024 13:16
@AlexWaygood AlexWaygood merged commit 5b4afd3 into main Oct 9, 2024
19 checks passed
@AlexWaygood AlexWaygood deleted the alex/remove-redundant-routines branch October 9, 2024 13:18
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 9, 2024

CodSpeed Performance Report

Merging #13682 will degrade performances by 4.13%

Comparing alex/remove-redundant-routines (ac777ae) with main (b9827a4)

Summary

❌ 1 regressions
✅ 31 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main alex/remove-redundant-routines Change
lexer[numpy/globals.py] 28.9 µs 30.1 µs -4.13%

@AlexWaygood
Copy link
Member Author

the codspeed flamegraph for this PR is very noisy so it's hard to tell, but I think that's just a false positive. I don't think I can see anything that would be plausibly related to this PR.

@AlexWaygood
Copy link
Member Author

Could you create an issue to re-visit the case-insensitive extension matching and assign it to the next milestone. We should re visit this soon.

I opened #13691

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants