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

Add type hints to util.py and tracer.py #1044

Merged
merged 3 commits into from
Sep 27, 2020

Conversation

Eric-Arellano
Copy link
Contributor

Also clean up some prior type hints to use cast, type vars, and type aliases.

self._lock = threading.RLock()
self._exists = os.path.exists
self._getenv = os.getenv
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unused

pex/tracer.py Outdated
self.msg = msg
self.verbosity = verbosity
self.parent = parent
if parent is not None:
parent.children.append(self)
self.children = []
parent.children.append(self) # type: ignore[has-type]
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 couldn't get MyPy happy with this, even with using cast(Trace, parent) and cast("List[Trace]", parent.children). It keeps complaining Cannot determine type of 'children' [has-type].

Copy link
Member

@jsirois jsirois Sep 26, 2020

Choose a reason for hiding this comment

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

Perhaps the idiom note above clears this up. If not, perhaps an isinstance check instead of a not None check. Hopefully the former.


class DistributionHelper(object):
@classmethod
def access_zipped_assets(cls, static_module_name, static_path, dir_location=None):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unused, beyond a test.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, this old thing. It is used but we don't have API markers yet. This commit's ba58a85 review is lost inside Twitter but was used to allow a Pexed up application that knew about Pexing to extract its assets; i.e: client code called (calls) it. This shouldn't be killed until API / deprecations / etc is set up. Perhaps add a note to this effect since you did the right thing given all the discoverable information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added back. Thanks.

@@ -86,18 +46,21 @@ def distribution_from_path(cls, path, name=None):
for dist in find_distributions(path):
if dist.project_name == name:
return dist
return None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MyPy wanted an explicit return.

Comment on lines -91 to +92
# type: (Optional[Union[str, int, bool]]) -> Optional[Union[str, int, bool]]
# type: (Optional[_T]) -> Optional[_T]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yay!!

pex/tracer.py Outdated
parent.children.append(self)
self.children = []
parent.children.append(self) # type: ignore[has-type]
self.children = cast("List[Trace]", [])
Copy link
Member

Choose a reason for hiding this comment

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

This is not idiomatic as I read the mypy docs. You want var = val # type: X for typing variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixed the type ignore. Thanks!

pex/tracer.py Outdated
self.msg = msg
self.verbosity = verbosity
self.parent = parent
if parent is not None:
parent.children.append(self)
self.children = []
parent.children.append(self) # type: ignore[has-type]
Copy link
Member

@jsirois jsirois Sep 26, 2020

Choose a reason for hiding this comment

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

Perhaps the idiom note above clears this up. If not, perhaps an isinstance check instead of a not None check. Hopefully the former.


class DistributionHelper(object):
@classmethod
def access_zipped_assets(cls, static_module_name, static_path, dir_location=None):
Copy link
Member

Choose a reason for hiding this comment

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

Ah, this old thing. It is used but we don't have API markers yet. This commit's ba58a85 review is lost inside Twitter but was used to allow a Pexed up application that knew about Pexing to extract its assets; i.e: client code called (calls) it. This shouldn't be killed until API / deprecations / etc is set up. Perhaps add a note to this effect since you did the right thing given all the discoverable information.

@@ -994,20 +994,26 @@ def test_pex_multi_resolve_2():
), "{} was not found in wheel".format(dist_substr)


if TYPE_CHECKING:
TEST_RESOLVE_FN = Callable[[str, str, str, str, Optional[str]], None]
Copy link
Member

Choose a reason for hiding this comment

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

All caps type names are jarring. I'm guessing you're trying to distinguish an alias from a "real" type via hungarian - but, by definition this is already all local to the file so I'm not seeing the point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking was it was a constant, so following the convention. But I like PascalCase more here.

@Eric-Arellano Eric-Arellano merged commit e67412c into pex-tool:master Sep 27, 2020
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.

None yet

2 participants