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

Improve type hints #206

Closed
wants to merge 1 commit into from

Conversation

TheTripleV
Copy link
Member

  1. Replace types like wpiutil._wpiutil.x with wpiutil.x
  2. Add support for fixing unit types (requires wpimath type caster change)
  3. Add type hints for buffer type

if sys.version_info >= (3, 12):
fp.write("\n\nfrom collections.abc import Buffer\n\n")
else:
fp.write("\n\nBuffer = typing.Union[bytes, bytearray, memoryview]\n\n")
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this use typing_extensions per the PEP? https://peps.python.org/pep-0688/#equivalent-for-older-python-versions

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah but that would be an extra dependency. Pure Commands will depend on typing_extensions. Idk if every robotpy-build project should too.

Copy link
Member

Choose a reason for hiding this comment

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

Wrap it in a try except ImportError then?

Comment on lines +171 to +174
if sys.version_info >= (3, 12):
fp.write("\n\nfrom collections.abc import Buffer\n\n")
else:
fp.write("\n\nBuffer = typing.Union[bytes, bytearray, memoryview]\n\n")
Copy link
Member

Choose a reason for hiding this comment

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

Version conditions for imports should go into the generated type stub, not here. Otherwise asking type checkers to target older versions will break.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did it that way since the wheels are version specific anyway. I'll put them in the stubs so it's the same everywhere.

Comment on lines +171 to +174
if sys.version_info >= (3, 12):
fp.write("\n\nfrom collections.abc import Buffer\n\n")
else:
fp.write("\n\nBuffer = typing.Union[bytes, bytearray, memoryview]\n\n")
Copy link
Member

Choose a reason for hiding this comment

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

This seems like something that should be contributed upstream, rather than us monkeypatching it in.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is an existing issue: sizmailov/pybind11-stubgen#87

But right now, I don't actually check if the type hints use buffer or if the type hint will override another custom type called buffer. That bit gets injected into every file. I would want to make sure the buffer stuff is only added when necessary+valid before upstreaming.


def write(self, s: str) -> None:
# fix underscored names
s = re.sub(r"([a-zA-Z0-9_]+)\._\1", r"\1", s)
Copy link
Member

Choose a reason for hiding this comment

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

What is this for?

Copy link
Member Author

Choose a reason for hiding this comment

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

When wpi* depends on wpiutil for example, import wpiutil._wpiutil is added to the top of the stubs. This changes that to import wpiutil. I haven't tested this yet but I'm hoping that it tricks intellisense into providing better imports.

Right now, when my students write code and let vscode auto import stuff, it always goes all the way to the underscored packages.

Copy link
Member

Choose a reason for hiding this comment

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

This feels like a really blunt instrument for doing that.

@auscompgeek
Copy link
Member

I vote to close this in favour of #211.

@auscompgeek
Copy link
Member

Obsolete by #211.

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

3 participants