-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
L2 layer typing #2863
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
L2 layer typing #2863
Conversation
d022fbe
to
c66bb94
Compare
Codecov Report
@@ Coverage Diff @@
## master #2863 +/- ##
==========================================
- Coverage 87.94% 85.35% -2.60%
==========================================
Files 255 249 -6
Lines 54630 53308 -1322
==========================================
- Hits 48046 45499 -2547
- Misses 6584 7809 +1225
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool PR, you didn't pick an easy one :p
You'll however have to wait for #2859 to be merged first and it'll trigger a few conflicts.
Thanks, I realized that in the middle, but then I was already invested :)
Sure, I understand. |
4201897
to
67236e5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, could you rebase against master? Thanks
No problem. |
67236e5
to
2d77355
Compare
2d77355
to
de06147
Compare
scapy/layers/l2.py
Outdated
@@ -408,41 +453,49 @@ class ARP(Packet): | |||
(val is None or valid_net6(val))) | |||
))), | |||
], | |||
StrFixedLenField("pdst", None, length_from=lambda pkt: pkt.plen), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're going to disable the mypy issues for this check (that requires casting from Any) on all files in layers/
(that are not Core scapy), because it's going to become such a mess. Will get back to this
To be perfectly honnest, we just started typing layers (most of the work was done on the core previously), so we're still trying to find good compromises between mypy rules and readable code. I'm sorry for that because I'm going to end up asking for tweaks several times. If that bothers you, I can take over your PR. Packet fields attributes (e.g. |
I've been fine with the requests so far. It allows me to learn a little about mypy and scapy.
Sure, I'd be happy too, but I've already rebased on top of #2954. Should I take the AnyField patch as well? |
Yes that would be nice. I'll try to have it merged ASAP |
de06147
to
e5545ec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is starting to look really good !
Could you please rebase against master? The two PRs have been merged
Ideally you could also squash all commits at this point in time. Thanks a lot !
Sequences in mypy allow variance, so we can assign lists of Field subtypes to fields_desc. This is needed for the scapy.layers.l2.Loopback class, which attempts to assign fields_desc twice with different types. Without this change, we get the following mypy errors: scapy/layers/l2.py:620: error: Incompatible types in assignment (expression has type "List[IntEnumField]", base class "Packet" defined the type as "List[Union[Field[Any, Any], _FieldContainer]]") scapy/layers/l2.py:620: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance scapy/layers/l2.py:620: note: Consider using "Sequence" instead, which is covariant
The current code stores a pair of the address and the current time, but this does not seem to match the cache's code where the time is stored in a different field.
routing_info is a StrLenField, and accepts a lambda (now that the fld argument was removed).
e5545ec
to
f0e7a7e
Compare
I squashed most of the patches, but I left a few that seemed independent. Let me know if you want to squash them too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from one last minor comment, it's good to be merged to me. Thanks a lot for your time
- Add typed attribute for Conf.neighbor in scapy.config. This allows mypy to acknowledge the existence of this attribute even though it is not initialized until the scapy.layers.l2 module is loaded. - Name the arp cache as a private global in layers.l2 To avoid mypy warnings about arp_cache not being a member of the NetCache class, store it in its own variable. - Do not accept None in ConditionalField's condition callback The callback is always called with a packet. - The MACField internal representation may be None Modify the types and the i2repr implementation accordingly. - Allow StrFixedLenField default argument to be None A None default is passed in scapy.layers.l2 for example. - Explicitly return None in Neighbor.resolve() - Use super() to access MACField.i2h When accessing the i2h method using an unbound method, mypy fails matching the types correctly. - Convert a few string literals to bytes literals in scapy.layers.l2 - ShortEnumField accepts Dict[str, int] as well as Dict[int, str] - Do not accept None in StrFixedLenField length_from callback The callback is almost always called with a packet, and most of its usage is with a lambda function that does not handle None. Add `type: ignore` comment to the randval method that does pass None to this callback (inside a try-except clause).
f0e7a7e
to
2660c8c
Compare
* Change type of Packet.fields_desc to a Sequence Sequences in mypy allow variance, so we can assign lists of Field subtypes to fields_desc. This is needed for the scapy.layers.l2.Loopback class, which attempts to assign fields_desc twice with different types. Without this change, we get the following mypy errors: scapy/layers/l2.py:620: error: Incompatible types in assignment (expression has type "List[IntEnumField]", base class "Packet" defined the type as "List[Union[Field[Any, Any], _FieldContainer]]") scapy/layers/l2.py:620: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance scapy/layers/l2.py:620: note: Consider using "Sequence" instead, which is covariant * Store just the hw address in the ARP cache in arping The current code stores a pair of the address and the current time, but this does not seem to match the cache's code where the time is stored in a different field. * Fix GRErouting.routing_info length routing_info is a StrLenField, and accepts a lambda (now that the fld argument was removed). * Add type annotations to scapy.layers.l2 - Add typed attribute for Conf.neighbor in scapy.config. This allows mypy to acknowledge the existence of this attribute even though it is not initialized until the scapy.layers.l2 module is loaded. - Name the arp cache as a private global in layers.l2 To avoid mypy warnings about arp_cache not being a member of the NetCache class, store it in its own variable. - Do not accept None in ConditionalField's condition callback The callback is always called with a packet. - The MACField internal representation may be None Modify the types and the i2repr implementation accordingly. - Allow StrFixedLenField default argument to be None A None default is passed in scapy.layers.l2 for example. - Explicitly return None in Neighbor.resolve() - Use super() to access MACField.i2h When accessing the i2h method using an unbound method, mypy fails matching the types correctly. - Convert a few string literals to bytes literals in scapy.layers.l2 - ShortEnumField accepts Dict[str, int] as well as Dict[int, str] - Do not accept None in StrFixedLenField length_from callback The callback is almost always called with a packet, and most of its usage is with a lambda function that does not handle None. Add `type: ignore` comment to the randval method that does pass None to this callback (inside a try-except clause).
* Change type of Packet.fields_desc to a Sequence Sequences in mypy allow variance, so we can assign lists of Field subtypes to fields_desc. This is needed for the scapy.layers.l2.Loopback class, which attempts to assign fields_desc twice with different types. Without this change, we get the following mypy errors: scapy/layers/l2.py:620: error: Incompatible types in assignment (expression has type "List[IntEnumField]", base class "Packet" defined the type as "List[Union[Field[Any, Any], _FieldContainer]]") scapy/layers/l2.py:620: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance scapy/layers/l2.py:620: note: Consider using "Sequence" instead, which is covariant * Store just the hw address in the ARP cache in arping The current code stores a pair of the address and the current time, but this does not seem to match the cache's code where the time is stored in a different field. * Fix GRErouting.routing_info length routing_info is a StrLenField, and accepts a lambda (now that the fld argument was removed). * Add type annotations to scapy.layers.l2 - Add typed attribute for Conf.neighbor in scapy.config. This allows mypy to acknowledge the existence of this attribute even though it is not initialized until the scapy.layers.l2 module is loaded. - Name the arp cache as a private global in layers.l2 To avoid mypy warnings about arp_cache not being a member of the NetCache class, store it in its own variable. - Do not accept None in ConditionalField's condition callback The callback is always called with a packet. - The MACField internal representation may be None Modify the types and the i2repr implementation accordingly. - Allow StrFixedLenField default argument to be None A None default is passed in scapy.layers.l2 for example. - Explicitly return None in Neighbor.resolve() - Use super() to access MACField.i2h When accessing the i2h method using an unbound method, mypy fails matching the types correctly. - Convert a few string literals to bytes literals in scapy.layers.l2 - ShortEnumField accepts Dict[str, int] as well as Dict[int, str] - Do not accept None in StrFixedLenField length_from callback The callback is almost always called with a packet, and most of its usage is with a lambda function that does not handle None. Add `type: ignore` comment to the randval method that does pass None to this callback (inside a try-except clause).
Checklist:
tox
or,cd test && ./run_tests_py2, cd test && ./run_tests_py3
)I tried adding type hints as suggested in #2158 for the l2 layer this time.
I didn't squash everything together as I thought it would be easier to review this way. Let me know if you prefer otherwise.
To make mypy happy, I had to make a few changes to the config, packet, and field modules: