-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Pathlib.iterdir semantics change dramatically under Python 3.13 #129871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Silly me. I just realized this issue probably isn't about a change to the generator expression but a change to pathlib. |
Indeed, the generator expression is not relevant:
|
Bisected to #107320 so confirming that it's related to |
I was even involved in that issue and had completely forgotten. So it was an intentional change, even though it doesn't appear in the "what's new". At the very least, we should add a note to "what's new", as this behavior might be surprising. I'm also thinking we should revisit our assumptions from #78722 in light of this new experience. It demonstrates that the change in behavior isn't necessarily beneficial and there may be other use-cases out there that are relying on the late evaluation of the error condition. I don't feel strongly about it, and I'm happy to proceed with this new behavior, but we should ask ourselves if a rollback is warranted to retain consistency across Python versions. |
I'll get a PR up for adding it to the whatsnew, and perhaps to the I still prefer the new behaviour - it meets most users expectations better, it makes exception handling much more straightforward, and it matches how |
This is indeed a big change. |
Maybe we should revert the |
I'm so conflicted on this, I can't even advocate for one approach over the other. I'm inclined to say we should raise the issue with a larger group. I do think it would be worthwhile to make a decision quickly to reduce the duration of exposure. Maybe just a post in Core Dev discuss? I'm happy to defer to your judgment Barney, but let me know if you'd like my help garnering a wider consensus. |
Bug report
Bug description:
This code behaves very differently on Python 3.13 than 3.12:
In Python 3.12, the generator expression was evaluated lazily, not invoking
path.iterdir()
until the generator was consumed. On 3.13, at least a portion of the generator is evaluated, triggering the exception when the dir does not exist.I discovered this issue in jaraco/jaraco.develop#26.
I looked at What's New for Python 3.13, and there's only one mention of generator expressions regarding mutation of locals in generator expressions, which doesn't seem to be relevant here.
That change mentions #74929, so maybe that change is also implicated in the change in execution order.
CPython versions tested on:
3.13
Operating systems tested on:
macOS
The text was updated successfully, but these errors were encountered: