Skip to content
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

Fixed Typehint for RunSettings.colocated_db_settings #462

Merged
merged 19 commits into from
Jan 29, 2024

Conversation

amandarichardsonn
Copy link
Contributor

This PR adds Python type hinting to RunSettings.colocated_db_settings

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c38f73f) 90.42% compared to head (73495ab) 81.42%.
Report is 7 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #462      +/-   ##
===========================================
- Coverage    90.42%   81.42%   -9.00%     
===========================================
  Files           60       60              
  Lines         3738     3834      +96     
===========================================
- Hits          3380     3122     -258     
- Misses         358      712     +354     
Files Coverage Δ
smartsim/_core/control/controller.py 78.48% <ø> (-8.65%) ⬇️
smartsim/_core/launcher/step/step.py 98.46% <100.00%> (ø)
smartsim/entity/model.py 78.84% <100.00%> (-16.58%) ⬇️
smartsim/settings/base.py 98.27% <100.00%> (+<0.01%) ⬆️

... and 23 files with indirect coverage changes

Copy link
Member

@MattToast MattToast left a comment

Choose a reason for hiding this comment

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

A couple of super pedantic nit picks, but otherwise looks about ready to go! 🚀

smartsim/entity/model.py Outdated Show resolved Hide resolved
smartsim/entity/model.py Outdated Show resolved Hide resolved
Comment on lines 100 to 101
db_cpus = int(self.colocated_db_settings.get("db_cpus", 0))
db_cpus = self.colocated_db_settings.get("db_cpus", 0)
assert isinstance(db_cpus, int)
Copy link
Member

Choose a reason for hiding this comment

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

Minor refactor request here to make sure this is not a breaking API change

db_cpus = int(t.cast(int, self.colocated_db_settings.get("db_cpus", 0)))
# and then you should be free to remove the `assert stmt` as well!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If Im already casting this to an int, why again?

Copy link
Member

@MattToast MattToast Jan 29, 2024

Choose a reason for hiding this comment

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

t.cast is purely for the type checker while the int will actually do the cast at runtime. It's HIGHLY unlikely, but there is the possibility that the "db_cpus" key maps to a float (or a str, or some other type) that can be cast to an int.

so for instance

self.colocated_db_settings = {"db_cpus": 3.0, ...}
db_cpus = int(self.colocated_db_settings.get("db_cpus", 0))
print(db_cpus)

will run just fine and print 3

while

self.colocated_db_settings = {"db_cpus": 3.0, ...}
db_cpus = self.colocated_db_settings.get("db_cpus", 0)
assert isinstance(db_cpus, int)
print(db_cpus)

will raise an AssertionError.

Just to make sure that this is preserving the original behavior and prevent an API break I would rather just subtly lie to the type checker and let it think that self.colocated_db_settings.get("db_cpus", 0) is some type that can be cast to an int. But do LMK if you think I am being too paranoid!

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 see I see, some witchery I would say

smartsim/settings/base.py Outdated Show resolved Hide resolved
smartsim/entity/model.py Outdated Show resolved Hide resolved
smartsim/entity/model.py Outdated Show resolved Hide resolved
smartsim/entity/model.py Outdated Show resolved Hide resolved
Comment on lines 371 to 376
x = t.cast(
t.Optional[t.Iterable[t.Union[int, t.Iterable[int]]]],
common_options.get("custom_pinning"),
)

colo_db_config = {}
e = t.cast(int, common_options.get("cpus"))
common_options["custom_pinning"] = self._create_pinning_string(x, e)
Copy link
Member

Choose a reason for hiding this comment

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

Can we change the names of these of these intermediary variables to something a bit more descriptive (maybe custom_pinning_ and cpus_)?

Or maybe just remove them entirely?

common_options["custom_pinning"] = self._create_pinning_string(
    t.cast(
        t.Optional[t.Iterable[t.Union[int, t.Iterable[int]]]], 
        common_options["custom_pinning"], common_options["cpus"]
    ),
    t.cast(int, common_options.get("cpus"))
)

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 would almost suggest keeping the vars for readability, but Im not as experienced so let me know!

Copy link
Member

Choose a reason for hiding this comment

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

Totally willing to keep the intermediate variables! I think I agree that they improve readability!

Copy link
Member

@MattToast MattToast left a comment

Choose a reason for hiding this comment

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

LGTM!! Thanks for helping improve our type safety!!

@amandarichardsonn amandarichardsonn merged commit 092163b into CrayLabs:develop Jan 29, 2024
34 checks passed
@amandarichardsonn amandarichardsonn deleted the mypie-colo branch January 29, 2024 23:24
ashao pushed a commit to ashao/SmartSim that referenced this pull request Jan 30, 2024
This PR adds Python type hinting to RunSettings.colocated_db_settings.

[ reviewed by @MattToast ]
[ committed by @amandarichardsonn ]
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