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

typehinting #653

Open
AiyionPrime opened this issue Jun 13, 2023 · 1 comment
Open

typehinting #653

AiyionPrime opened this issue Jun 13, 2023 · 1 comment
Labels

Comments

@AiyionPrime
Copy link
Contributor

AiyionPrime commented Jun 13, 2023

In order to make this library more useful to modern python codebases, I'd like to propose the use of typehinting. (Which might not look straight forward, as we'd technically try to typehint classes with dynamically created attributes.)

One performant option would be the style of pep526, which introduced instance variable type annotations on the classes level.
It looks similar to class variables, but is not.

class BasicStarshipProtocol(dpkt.Packet):
    damage: int                           # instance variable without default

https://peps.python.org/pep-0526/#class-and-instance-variable-annotations

While this is what I'm doing on local codebases dpkt.Packet does not provide any form of typehints yet.

This certainly won't happen before we start working on #551.


A workaround would be to provide .pyi stub files for the proptocols.
We could place them alongside the python files and python2 should ignore them, while python3 will pick up the hints.

I'll open a PR for the latter, which introduces an exemplary .pyi file for dpkts packet.

Another option (if typehints were rejected in this repo) would be to track such stub files in an external repo. (hopefully not necessary)

https://peps.python.org/pep-0484/#storing-and-distributing-stub-files

I really would like to hear what you think about typed Protocol Packets.

@kbandla
Copy link
Owner

kbandla commented Dec 18, 2023

@AiyionPrime this has been discussed and would surely be nice to have. To tackle this we first need to finish #551. Once we do that we can surely take this on.
I much prefer typehints added to code than to maintain .pyi files. Also note that dpkt uses metaclasses heavily and some of the types are dynamically built at runtime, so even with typehinting some IDEs might not be able to generate typeahead suggestions. Will keep this issue open for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants