-
Notifications
You must be signed in to change notification settings - Fork 8
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
Type stubs #4
Comments
Indeed, would be great to have types in the code. |
@mina86 so I think we can either put them inline (which would mean making the library require Python >3.5), or add a Any preference? |
I say inline. With pygtrie I’m somewhat conservative with required Python version and with 3.8 still getting security updates I’d like to keep the version ≤ 3.8. |
FYI, just pushed c0dd534 which removes 2.x compatibility. (Honestly I thought it was already upstream). |
Just FYI, you can have inline type annotations for Python <3.5 using type comments. You can also include and ship stubs directly with the library if that becomes too unwieldy. And finally there's many ways to support more modern types and annotations for older python syntax (< 3.10):
You would also still need to add a |
It’s probably not worth the effort though. Using comments is considerably less ergonomic.
I’d rather have it all in the source code because that makes maintenance easier. Less chance to forget to update types when editing source code. It also aids reading the code since type hints serve as documentation.
Yes. In general my attitude is that the code should execute in as old version as Python as is not too burdensome to support but it’s fine if latest Python version is required to get the most benefits of the types. I’m also not concerned with runtime availability of the type annotations (unless someone comes and opens an Issue for that; then I’ll think about it). |
Great! I'll open a PR with the types I added for my purposes, then I'll leave it up to you whether you want to merge as is or collaborate on a separate branch to add types for everything. @Avasam I think these are all okay with 3.5, right? Feedback also welcome :) from typing import Callable, Generic, Iterable, TypeVar
C = TypeVar('C')
V = TypeVar('V')
Children = Iterable[C]
Path = tuple[str, ...]
PathConv = Callable[[Path], str]
NodeFactory = Callable[[PathConv, Path, Children[C], V], C]
class StringTrie(Generic[V]):
def __init__(self, separator: str | None = None) -> None: ...
def __setitem__(self, key: str, value: V) -> None: ...
def __getitem__(self, key: str | slice) -> V: ...
def keys(self, prefix: str | None = None,
shallow: bool = False) -> list[str]: ...
def values(self, prefix: str | None = None,
shallow: bool = False) -> list[V]: ...
def items(self, prefix: str | None = None,
shallow: bool = False) -> list[tuple[str, V]]: ...
def traverse(self, node_factory: NodeFactory[C, V]) -> C: ...
def enable_sorting(self, enable: bool) -> None: ... |
I think that’s 3.9 syntax or something. This should be
And that’s Python 3.10. For
The default value for the separator is |
I can never remember by hearth past Python 3.7 . The best way is to actually try it. Some things I notice immediately: collections types are not subscriptable in Python 3.8, you'll have to import from typing. The union operator ( As long as Python 3.6 is supported in the project, you can't even use You can work around some annotation issues with stringified annotations (ie: (all comments above only apply for inline types, stub files wouldn't matter) You could also forego some type aliases, especially if they risk being confused with other types (ie: I just noticed this project is a single file-module. you'll have to make it a package ( You can (should) automate testing this. The best thing to do at the very least is to have some smoke-screen tests for all module that do nothing but import. To ensure no import or syntax error on Python 3.5. Tooling-wise you'd have to use outdated versions to check all the way down to 3.5 . I'd still at least recommend testing with type-checkers, to make sure your annotations work for your users:
And optionally type/syntax linters like:
All of the linters mentioned above have been re-implemented in Ruff, which I'd highly suggest instead (simpler, faster, and you can also disable rules that try to enforce a syntax too modern, unlike pyupgrade) Even if you still need to manually validate for 3.5-3.6, you can at least automatically ensure and enforce that you'll write code fully compatible with >=3.7 If you have typing questions, feel free to ask over at their discussion forum or try the typing chat room on gitter.im (links taken from https://github.com/python/typeshed#discussion) |
I double checked, (optional as well) You should also prefer explicitly marking your type aliases as such. Checked by Flake8-PYI rule Y026. But on Python <3.10 that will require a dependency on |
FYI I've put some stubs up here: python/typeshed#10796
But maybe want them in this repo?
The text was updated successfully, but these errors were encountered: