Skip to content

Minor cleanup #861

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
May 19, 2025
Merged

Minor cleanup #861

merged 1 commit into from
May 19, 2025

Conversation

dandavison
Copy link
Contributor

Some minor cleanup pulled out of Nexus work.

@dandavison dandavison requested a review from a team as a code owner May 8, 2025 22:43
Comment on lines -1546 to +1593
handle: Optional[_ActivityHandle] = None
handle: _ActivityHandle
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, can the static analyzer know that this is set before used? Or do the Python static analyzers not check whether a value is set before use?

Copy link
Contributor Author

@dandavison dandavison May 16, 2025

Choose a reason for hiding this comment

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

They can't detect all situations where it is read before being set. So in that case it would be a run-time error, similar to the AssertionError that would have been thrown by the code prior to this commit. Since the code algorithmically guarantees that these run-time errors do not in fact occur, IMO it is more idiomatic and cleaner not to set an initial value that is never used.

Copy link
Member

Choose a reason for hiding this comment

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

IMO it is more idiomatic and cleaner not to set an initial value that is never used

Never used now but you have no protection anymore. I think it's safer to only ever capture variables in a nested function that have been initialized, even if you promise to re-initialize them again later. And nonlocal and assert signal this intent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's safer

How is it safer -- it's a run-time error either way, either an AssertionError or a NameError / some sort of unbound variable error. Either way, the problem will be simple to track down.

Copy link
Member

@cretz cretz May 16, 2025

Choose a reason for hiding this comment

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

It's safer from a general logic POV to have no possibility of uninitialized variable access (in any programming language). And it's also clearer from a reader POV that you intended for this situation via nonlocal/assert, which helps safety/comprehension. Python is not smart enough to enforce accessing an only initialized variables like some statically typed languages do, hence the help. Sure it may not matter much here, definitely not enough to change from what it was, but I'm not pushing back on you changing it.

@dandavison dandavison force-pushed the pre-nexus-refactoring branch 3 times, most recently from 4688467 to ffeb684 Compare May 17, 2025 13:45
@dandavison dandavison force-pushed the pre-nexus-refactoring branch from ffeb684 to 70c847a Compare May 18, 2025 18:07
@dandavison dandavison merged commit 1b136a0 into main May 19, 2025
18 checks passed
@dandavison dandavison deleted the pre-nexus-refactoring branch May 19, 2025 12:59
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.

3 participants