Skip to content

Commit 42115bf

Browse files
p-l-gpotter2
andauthored
Drop six library (third & last batch) (#3857)
* Drop six library (third & last batch) * Fix tiny mistake * Lots and lots of types fixes (+ upg 2 mypy 1.1.1) --------- Co-authored-by: gpotter2 <[email protected]>
1 parent cb619e5 commit 42115bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+618
-1712
lines changed

.config/mypy/mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Internal Scapy modules that we ignore
44

5-
[mypy-scapy.libs.six,scapy.libs.six.moves,scapy.libs.winpcapy]
5+
[mypy-scapy.libs.winpcapy]
66
ignore_errors = True
77
ignore_missing_imports = True
88

.config/mypy/mypy_check.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
x not in {
102102
# Disabled on Windows
103103
"scapy/arch/linux.py",
104+
"scapy/arch/unix.py",
104105
"scapy/arch/solaris.py",
105106
"scapy/contrib/cansocket_native.py",
106107
"scapy/contrib/isotp/isotp_native_socket.py",
@@ -115,4 +116,4 @@
115116
os.path.abspath(f) for f in FILES
116117
]
117118

118-
mypy_main(None, sys.stdout, sys.stderr, ARGS)
119+
mypy_main(args=ARGS)

.github/workflows/unittests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Setup Python
5353
uses: actions/setup-python@v4
5454
with:
55-
python-version: "3.10"
55+
python-version: "3.11"
5656
- name: Install tox
5757
run: pip install tox
5858
- name: Run mypy

CONTRIBUTING.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ of function calls, packet creations, etc.).
6464
is a nice read!
6565

6666
- Avoid creating unnecessary `list` objects, particularly if they
67-
can be huge (e.g., when possible, use `scapy.modules.six.range()` instead of
68-
`range()`, `for line in fdesc` instead of `for line in
69-
fdesc.readlines()`; more generally prefer generators over lists).
67+
can be huge (e.g., when possible, use `for line in fdesc` instead of
68+
`for line in fdesc.readlines()`; more generally prefer generators over
69+
lists).
7070

7171
### Tests
7272

@@ -146,15 +146,11 @@ The project aims to provide code that works both on Python 2 and Python 3. There
146146
- lambdas must be written using a single argument when using tuples: use `lambda x, y: x + f(y)` instead of `lambda (x, y): x + f(y)`.
147147
- use int instead of long
148148
- use list comprehension instead of map() and filter()
149-
- use scapy.modules.six.moves.range instead of xrange and range
150-
- use scapy.modules.six.itervalues(dict) instead of dict.values() or dict.itervalues()
151-
- use scapy.modules.six.string_types instead of basestring
152149
- `__bool__ = __nonzero__` must be used when declaring `__nonzero__` methods
153150
- `__next__ = next` must be used when declaring `next` methods in iterators
154151
- `StopIteration` must NOT be used in generators (but it can still be used in iterators)
155152
- `io.BytesIO` must be used instead of `StringIO` when using bytes
156153
- `__cmp__` must not be used.
157-
- UserDict should be imported via `six.UserDict`
158154

159155
### Code review
160156

scapy/ansmachine.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Answering machines #
1212
########################
1313

14+
import abc
1415
import functools
1516
import socket
1617
import warnings
@@ -30,14 +31,13 @@
3031
Tuple,
3132
Type,
3233
TypeVar,
33-
_Generic_metaclass,
3434
cast,
3535
)
3636

3737
_T = TypeVar("_T", Packet, PacketList)
3838

3939

40-
class ReferenceAM(_Generic_metaclass):
40+
class ReferenceAM(type):
4141
def __new__(cls,
4242
name, # type: str
4343
bases, # type: Tuple[type, ...]
@@ -142,9 +142,10 @@ def is_request(self, req):
142142
# type: (Packet) -> int
143143
return 1
144144

145+
@abc.abstractmethod
145146
def make_reply(self, req):
146147
# type: (Packet) -> _T
147-
return req
148+
pass
148149

149150
def send_reply(self, reply, send_function=None):
150151
# type: (_T, Optional[Callable[..., None]]) -> None
@@ -268,4 +269,4 @@ def sniff(self):
268269

269270
def make_reply(self, req, address=None):
270271
# type: (Packet, Optional[Any]) -> Packet
271-
return super(AnsweringMachineTCP, self).make_reply(req)
272+
return req

scapy/arch/libpcap.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def next(self):
364364
float(self.header.contents.ts.tv_usec) / 1e6
365365
)
366366
pkt = bytes(bytearray(
367-
self.pkt_data[:self.header.contents.len] # type: ignore
367+
self.pkt_data[:self.header.contents.len]
368368
))
369369
return ts, pkt
370370
__next__ = next
@@ -502,7 +502,7 @@ def __init__(self,
502502
self.pcap_fd.setfilter(filter)
503503

504504
def send(self, x):
505-
# type: (int) -> NoReturn
505+
# type: (Packet) -> NoReturn
506506
raise Scapy_Exception(
507507
"Can't send anything with L2pcapListenSocket"
508508
)

scapy/arch/windows/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
# hot-patching socket for missing variables on Windows
7777
if not hasattr(socket, 'IPPROTO_IPIP'):
78-
socket.IPPROTO_IPIP = 4
78+
socket.IPPROTO_IPIP = 4 # type: ignore
7979
if not hasattr(socket, 'IP_RECVTTL'):
8080
socket.IP_RECVTTL = 12 # type: ignore
8181
if not hasattr(socket, 'IPV6_HDRINCL'):
@@ -86,7 +86,7 @@
8686
if not hasattr(socket, 'SOL_IPV6'):
8787
socket.SOL_IPV6 = socket.IPPROTO_IPV6 # type: ignore
8888
if not hasattr(socket, 'IPPROTO_GRE'):
89-
socket.IPPROTO_GRE = 47
89+
socket.IPPROTO_GRE = 47 # type: ignore
9090
if not hasattr(socket, 'IPPROTO_AH'):
9191
socket.IPPROTO_AH = 51
9292
if not hasattr(socket, 'IPPROTO_ESP'):
@@ -965,8 +965,8 @@ def _route_add_loopback(routes=None, # type: Optional[List[Any]]
965965
if iface == conf.loopback_name:
966966
conf.route.routes.remove(route)
967967
# Remove conf.loopback_name interface
968-
for devname, iface in list(conf.ifaces.items()):
969-
if iface == conf.loopback_name:
968+
for devname, ifname in list(conf.ifaces.items()):
969+
if ifname == conf.loopback_name:
970970
conf.ifaces.pop(devname)
971971
# Inject interface
972972
conf.ifaces["{0XX00000-X000-0X0X-X00X-00XXXX000XXX}"] = adapter

scapy/asn1/asn1.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
Optional,
2727
Tuple,
2828
Type,
29-
TypeVar,
3029
Union,
31-
_Generic_metaclass,
3230
cast,
3331
TYPE_CHECKING,
3432
)
33+
from typing import (
34+
TypeVar,
35+
)
3536

3637
if TYPE_CHECKING:
3738
from scapy.asn1.ber import BERcodec_Object
@@ -278,7 +279,7 @@ class ASN1_Class_UNIVERSAL(ASN1_Class):
278279
TIME_TICKS = cast(ASN1Tag, 3 | 0x40) # application-specific encoding
279280

280281

281-
class ASN1_Object_metaclass(_Generic_metaclass):
282+
class ASN1_Object_metaclass(type):
282283
def __new__(cls,
283284
name, # type: str
284285
bases, # type: Tuple[type, ...]

scapy/asn1/ber.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
Type,
4040
TypeVar,
4141
Union,
42-
_Generic_metaclass,
4342
cast,
4443
)
4544

@@ -263,7 +262,7 @@ def BER_tagging_enc(s, hidden_tag=None, implicit_tag=None, explicit_tag=None):
263262
# [ BER classes ] #
264263

265264

266-
class BERcodec_metaclass(_Generic_metaclass):
265+
class BERcodec_metaclass(type):
267266
def __new__(cls,
268267
name, # type: str
269268
bases, # type: Tuple[type, ...]

scapy/asn1fields.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@
5252
Optional,
5353
Tuple,
5454
Type,
55-
TypeVar,
5655
Union,
5756
cast,
5857
TYPE_CHECKING,
5958
)
59+
from typing import (
60+
TypeVar,
61+
)
6062

6163
if TYPE_CHECKING:
6264
from scapy.asn1packet import ASN1_Packet
@@ -181,7 +183,7 @@ def extract_packet(self,
181183
try:
182184
c = cls(s, _underlayer=_underlayer)
183185
except ASN1F_badsequence:
184-
c = packet.Raw(s, _underlayer=_underlayer)
186+
c = packet.Raw(s, _underlayer=_underlayer) # type: ignore
185187
cpad = c.getlayer(packet.Raw)
186188
s = b""
187189
if cpad is not None:
@@ -229,8 +231,8 @@ def __str__(self):
229231
return repr(self)
230232

231233
def randval(self):
232-
# type: () -> RandField[Any]
233-
return RandInt()
234+
# type: () -> RandField[_I]
235+
return cast(RandField[_I], RandInt())
234236

235237

236238
############################
@@ -527,7 +529,7 @@ def __init__(self,
527529
):
528530
# type: (...) -> None
529531
if isinstance(cls, type) and issubclass(cls, ASN1F_field):
530-
self.fld = cast(Type[ASN1F_field[Any, Any]], cls)
532+
self.fld = cls
531533
self._extract_packet = lambda s, pkt: self.fld(
532534
self.name, b"").m2i(pkt, s)
533535
self.holds_packets = 0
@@ -696,7 +698,6 @@ def __init__(self, name, default, *args, **kwargs):
696698
else:
697699
self.choices[p.ASN1_root.network_tag] = p
698700
elif hasattr(p, "ASN1_tag"):
699-
p = cast(Union[ASN1F_PACKET, Type[ASN1F_field[Any, Any]]], p)
700701
if isinstance(p, type):
701702
# should be ASN1F_field class
702703
self.choices[int(p.ASN1_tag)] = p
@@ -735,9 +736,8 @@ def m2i(self, pkt, s):
735736
)
736737
)
737738
if hasattr(choice, "ASN1_root"):
738-
choice = cast('ASN1_Packet', choice)
739739
# we don't want to import ASN1_Packet in this module...
740-
return self.extract_packet(choice, s, _underlayer=pkt)
740+
return self.extract_packet(choice, s, _underlayer=pkt) # type: ignore
741741
elif isinstance(choice, type):
742742
return choice(self.name, b"").m2i(pkt, s)
743743
else:
@@ -767,7 +767,7 @@ def randval(self):
767767
elif hasattr(p, "ASN1_tag"):
768768
if isinstance(p, type):
769769
# should be (basic) ASN1F_field class
770-
randchoices.append(p("dummy", None).randval()) # type: ignore
770+
randchoices.append(p("dummy", None).randval())
771771
else:
772772
# should be ASN1F_PACKET instance
773773
randchoices.append(p.randval())
@@ -841,7 +841,7 @@ def i2m(self,
841841
implicit_tag=self.implicit_tag,
842842
explicit_tag=self.explicit_tag)
843843

844-
def randval(self):
844+
def randval(self): # type: ignore
845845
# type: () -> ASN1_Packet
846846
return packet.fuzz(self.cls())
847847

@@ -863,8 +863,10 @@ def __init__(self,
863863
):
864864
# type: (...) -> None
865865
self.cls = cls
866-
super(ASN1F_BIT_STRING_ENCAPS, self).__init__(
867-
name, default and raw(default), context=context,
866+
super(ASN1F_BIT_STRING_ENCAPS, self).__init__( # type: ignore
867+
name,
868+
default and raw(default),
869+
context=context,
868870
implicit_tag=implicit_tag,
869871
explicit_tag=explicit_tag
870872
)

scapy/asn1packet.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def __new__(cls,
3434
# type: (...) -> Type[ASN1_Packet]
3535
if dct["ASN1_root"] is not None:
3636
dct["fields_desc"] = dct["ASN1_root"].get_fields_list()
37-
return super(ASN1Packet_metaclass, cls).__new__(cls, name, bases, dct)
37+
return cast(
38+
'Type[ASN1_Packet]',
39+
super(ASN1Packet_metaclass, cls).__new__(cls, name, bases, dct),
40+
)
3841

3942

4043
class ASN1_Packet(Packet, metaclass=ASN1Packet_metaclass):

scapy/automaton.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
Type,
5151
TypeVar,
5252
Union,
53-
_Generic_metaclass,
5453
cast,
5554
)
5655

@@ -134,7 +133,7 @@ def select_objects(inputs, remain):
134133
_T = TypeVar("_T")
135134

136135

137-
class ObjectPipe(Generic[_T], metaclass=_Generic_metaclass):
136+
class ObjectPipe(Generic[_T]):
138137
def __init__(self, name=None):
139138
# type: (Optional[str]) -> None
140139
self.name = name or "ObjectPipe"
@@ -626,7 +625,7 @@ def __init__(self,
626625
self.atmt.runbg()
627626

628627
def send(self, s):
629-
# type: (bytes) -> int
628+
# type: (Union[bytes, Packet]) -> int
630629
if not isinstance(s, bytes):
631630
s = bytes(s)
632631
return self.spa.send(s)
@@ -694,7 +693,7 @@ def __new__(cls, name, bases, dct):
694693
while classes:
695694
c = classes.pop(0) # order is important to avoid breaking method overloading # noqa: E501
696695
classes += list(c.__bases__)
697-
for k, v in c.__dict__.items():
696+
for k, v in c.__dict__.items(): # type: ignore
698697
if k not in members:
699698
members[k] = v
700699

@@ -902,9 +901,9 @@ def __init__(self,
902901
):
903902
# type: (...) -> None
904903
if rd is not None and not isinstance(rd, (int, ObjectPipe)):
905-
rd = rd.fileno() # type: ignore
904+
rd = rd.fileno()
906905
if wr is not None and not isinstance(wr, (int, ObjectPipe)):
907-
wr = wr.fileno() # type: ignore
906+
wr = wr.fileno()
908907
self.rd = rd
909908
self.wr = wr
910909

@@ -998,8 +997,8 @@ class Singlestep(AutomatonStopped):
998997

999998
class InterceptionPoint(AutomatonStopped):
1000999
def __init__(self, msg, state=None, result=None, packet=None):
1001-
# type: (str, Optional[Message], Optional[str], Optional[str]) -> None # noqa: E501
1002-
Automaton.AutomatonStopped.__init__(self, msg, state=state, result=result) # noqa: E501
1000+
# type: (str, Optional[Message], Optional[str], Optional[Packet]) -> None
1001+
Automaton.AutomatonStopped.__init__(self, msg, state=state, result=result)
10031002
self.packet = packet
10041003

10051004
class CommandMessage(AutomatonException):

scapy/autorun.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def autorun_get_interactive_session(cmds, **kargs):
167167
try:
168168
try:
169169
sys.stdout = sys.stderr = sw
170-
sys.excepthook = sys.__excepthook__ # type: ignore
170+
sys.excepthook = sys.__excepthook__
171171
res = autorun_commands_timeout(cmds, **kargs)
172172
except StopAutorun as e:
173173
e.code_run = sw.s

0 commit comments

Comments
 (0)