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

Stricter pyright Setup #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Stricter pyright Setup #24

wants to merge 1 commit into from

Conversation

alberthli
Copy link
Contributor

Add more type checks with pyright. This is a first stab just to gut check how much of a pain it is to enforce this officially vs. just as a strong suggestion for documentation (hint: it was pretty annoying).

@alberthli alberthli marked this pull request as draft November 12, 2023 07:34
@alberthli
Copy link
Contributor Author

alberthli commented Nov 12, 2023

@vincekurtz I realized when playing around with pyright tonight that we had really loose settings on type checking on main (not even talking about the strict setting, which is super pedantic about everything). When I adjusted this, there were a lot of errors that came up, many of them inscrutable and fairly difficult to solve. I ended up hacking the last few just to forcibly pass the pyright checks.

Here's my philosophy on this right now. Type hints are really useful, but are still just hints. I don't think we should strongly enforce typing (especially during CI), but I do think we should keep up a standard of typing things (and calling people out in review).

I also think that locally, it's not a bad idea to run pyright with slightly stricter settings because it actually does catch confusing typing, and this is useful to fix for clarity. However, more often than not, it complains about things which have basically no effect on clarity, and just kills dev time. About ~20% of the changes I made in this demonstration PR (not to be merged) were actually useful.

I put some comments on the useful examples that the stricter pyright settings caught below.

@@ -4,7 +4,7 @@
from ambersim import ROOT


def _check_filepath(filepath: Union[str, Path]) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Example: pyright complains about _check_filepath because the leading underscore implies it's a private function of the current file, but it's unused within the file. Further, I import this elsewhere, so it's clearly not private.

@@ -69,7 +75,7 @@ def load_mj_model_from_file(
output_name = "/".join(str(filepath).split("/")[:-1]) + "/_temp_xml_model.xml"
save_model_xml(filepath, output_name=output_name)
mj_model = _modify_robot_float_base(output_name)
Path.unlink(output_name)
Path(output_name).unlink()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Example: this is actually just a straight up error that I resolve in a sibling branch, but I have no idea how it passed CI for so long. It actually occurs in a few other places, too.

"reportMissingTypeStubs": false,
"reportGeneralTypeIssues": false,
"reportGeneralTypeIssues": true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

reportGeneralTypeIssues: this is the main setting change that caused all these "errors" to pop up that I fixed.

Comment on lines +21 to +25
def _modify_robot_float_base(filepath: str) -> mj.MjModel:
"""Modifies a robot to have a floating base if it doesn't already."""
# loading current robot
assert str(filepath).split(".")[-1] == "xml"
robot = mjcf.from_file(filepath, model_dir="/".join(filepath.split("/")[:-1]))
robot = from_file(filepath, model_dir="/".join(filepath.split("/")[:-1]))
Copy link
Contributor Author

@alberthli alberthli Nov 12, 2023

Choose a reason for hiding this comment

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

Example: I just copy/pasted this type hint from other function signatures, but pyright complains that objects of type Path don't have a split function. It turns out that I only ever pass in str args into this function, so I changed the type hint to reflect that.

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.

1 participant