Skip to content

Add type annotations to runfiles library #764

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 2 commits into from
Aug 14, 2022
Merged

Conversation

dzbarsky
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe: Type annotations/documentation

What is the current behavior?

No annotations currently

Issue Number: N/A

What is the new behavior?

Annotations

Does this PR introduce a breaking change?

  • Yes
  • No

No, unless we need to support very old python versions without typing in the stdlib?

Other information

@dzbarsky dzbarsky requested review from brandjon and lberki as code owners July 22, 2022 07:31
@@ -68,16 +68,20 @@
import os
import posixpath

from typing import Callable, Dict, Optional, Tuple, Union
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need the import if only using type comments and not annotations?
Will this make it backwards incompatible with older versions of Python that users may be using?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately yes. If we don't import this, we get errors like so:

external/rules_python/python/runfiles/runfiles.py:81: error:
Name "Optional" is not defined  [name-defined]
    def Create(env=None):

typing has been in the stdlib since 3.5, but for older pythons you would need typing from pypi. I'll just import this behind an if False and leave a comment explaining why

Copy link
Contributor

Choose a reason for hiding this comment

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

If you're forced to import these, why not actually annotate the functions? instead of using comments? Doesn't this break backward compatibility with the long-since-EOL python2 if you need the imports? Might as well add proper annotations, right?

Copy link
Contributor Author

@dzbarsky dzbarsky Aug 2, 2022

Choose a reason for hiding this comment

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

I've updated this PR to not result in any runtime import changes, this is an outdated version. If we want to drop py2 there's probably a ton of cleanup than can be done, but I suspect we should do that in a more principled way than sneaking it in this PR. (In addition, the typing imports don't exist before 3.5 anyway)

Copy link
Collaborator

@groodt groodt left a comment

Choose a reason for hiding this comment

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

LGTM

@groodt groodt merged commit a2b7f42 into bazel-contrib:main Aug 14, 2022
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.

4 participants