Skip to content

Prefer exist_ok=True over explicit os.path.exists(...) checks #16642

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

Merged
merged 1 commit into from
Dec 9, 2023

Conversation

lgeiger
Copy link
Contributor

@lgeiger lgeiger commented Dec 9, 2023

This PR replaces explicit os.path.exists(...) checks with os.makedirs(..., exist_ok=True) where possible.

This removes the need for an extra existence check and slightly simplifies the code. This can also prevent race conditions like #16630.

Copy link
Contributor

github-actions bot commented Dec 9, 2023

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

I got worried that this might not work on all versions, but exist_ok=True has existed since 3.2, so this is fine. Thank you!

@JelleZijlstra JelleZijlstra merged commit 0567da9 into python:master Dec 9, 2023
@lgeiger lgeiger deleted the makedirs-exist branch December 9, 2023 22:07
kourbou pushed a commit to kourbou/mypy that referenced this pull request Dec 11, 2023
…thon#16642)

This PR replaces explicit `os.path.exists(...)` checks with
`os.makedirs(..., exist_ok=True)` where possible.

This removes the need for an extra existence check and slightly
simplifies the code. This can also prevent race conditions like python#16630.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants