Skip to content

Commit 2c9816e

Browse files
Add stubs for netaddr (#9431)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 64e02a0 commit 2c9816e

22 files changed

+757
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Error: is not present in stub
2+
# =============================
3+
netaddr.core.a # This is a temporary module attribute used to detect python version
4+
5+
# These are unnecessary re-exports
6+
netaddr.ip.INET_PTON
7+
netaddr.ip.N
8+
netaddr.ip.NOHOST
9+
netaddr.ip.P
10+
netaddr.ip.Z
11+
netaddr.ip.ZEROFILL
12+
13+
14+
# Error: is not present at runtime
15+
# ================================
16+
netaddr.ip.iana.XMLRecordParser.__getattr__ # __init__ has `self.__dict__.update(kwargs)`

stubs/netaddr/METADATA.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version = "0.8.*"
2+
3+
[tool.stubtest]
4+
ignore_missing_stub = false

stubs/netaddr/netaddr/__init__.pyi

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from netaddr.contrib.subnet_splitter import SubnetSplitter as SubnetSplitter
2+
from netaddr.core import (
3+
INET_PTON as INET_PTON,
4+
NOHOST as NOHOST,
5+
ZEROFILL as ZEROFILL,
6+
AddrConversionError as AddrConversionError,
7+
AddrFormatError as AddrFormatError,
8+
N as N,
9+
NotRegisteredError as NotRegisteredError,
10+
P as P,
11+
Z as Z,
12+
)
13+
from netaddr.eui import EUI as EUI, IAB as IAB, OUI as OUI
14+
from netaddr.ip import (
15+
IPAddress as IPAddress,
16+
IPNetwork as IPNetwork,
17+
IPRange as IPRange,
18+
all_matching_cidrs as all_matching_cidrs,
19+
cidr_abbrev_to_verbose as cidr_abbrev_to_verbose,
20+
cidr_exclude as cidr_exclude,
21+
cidr_merge as cidr_merge,
22+
iprange_to_cidrs as iprange_to_cidrs,
23+
iter_iprange as iter_iprange,
24+
iter_unique_ips as iter_unique_ips,
25+
largest_matching_cidr as largest_matching_cidr,
26+
smallest_matching_cidr as smallest_matching_cidr,
27+
spanning_cidr as spanning_cidr,
28+
)
29+
from netaddr.ip.glob import (
30+
IPGlob as IPGlob,
31+
cidr_to_glob as cidr_to_glob,
32+
glob_to_cidrs as glob_to_cidrs,
33+
glob_to_iprange as glob_to_iprange,
34+
glob_to_iptuple as glob_to_iptuple,
35+
iprange_to_globs as iprange_to_globs,
36+
valid_glob as valid_glob,
37+
)
38+
from netaddr.ip.nmap import iter_nmap_range as iter_nmap_range, valid_nmap_range as valid_nmap_range
39+
from netaddr.ip.rfc1924 import base85_to_ipv6 as base85_to_ipv6, ipv6_to_base85 as ipv6_to_base85
40+
from netaddr.ip.sets import IPSet as IPSet
41+
from netaddr.strategy.eui48 import (
42+
mac_bare as mac_bare,
43+
mac_cisco as mac_cisco,
44+
mac_eui48 as mac_eui48,
45+
mac_pgsql as mac_pgsql,
46+
mac_unix as mac_unix,
47+
mac_unix_expanded as mac_unix_expanded,
48+
valid_str as __eui48_valid_str,
49+
)
50+
from netaddr.strategy.eui64 import (
51+
eui64_bare as eui64_bare,
52+
eui64_base as eui64_base,
53+
eui64_cisco as eui64_cisco,
54+
eui64_unix as eui64_unix,
55+
eui64_unix_expanded as eui64_unix_expanded,
56+
valid_str as __eui64_valid_str,
57+
)
58+
from netaddr.strategy.ipv4 import valid_str as __ipv4_valid_str
59+
from netaddr.strategy.ipv6 import (
60+
ipv6_compact as ipv6_compact,
61+
ipv6_full as ipv6_full,
62+
ipv6_verbose as ipv6_verbose,
63+
valid_str as __ipv6_valid_str,
64+
)
65+
66+
# These are reexported with different names
67+
valid_ipv4 = __ipv4_valid_str
68+
valid_ipv6 = __ipv6_valid_str
69+
valid_mac = __eui48_valid_str
70+
valid_eui64 = __eui64_valid_str
71+
72+
# Module constants
73+
__version__: str
74+
VERSION: tuple[int, ...]
75+
STATUS: str

stubs/netaddr/netaddr/cli.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from netaddr import *
2+
3+
def main() -> None: ...

stubs/netaddr/netaddr/compat.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Python 2 compatibility module
2+
# All members are prefixed with "_", nothing to declare.

stubs/netaddr/netaddr/contrib/__init__.pyi

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from netaddr.ip import IPNetwork, _IPAddressAddr
2+
3+
class SubnetSplitter:
4+
def __init__(self, base_cidr: _IPAddressAddr) -> None: ...
5+
def extract_subnet(self, prefix: int, count: int | None = ...) -> list[IPNetwork]: ...
6+
def available_subnets(self) -> list[IPNetwork]: ...
7+
def remove_subnet(self, ip_network: IPNetwork) -> None: ...

stubs/netaddr/netaddr/core.pyi

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from _typeshed import Incomplete, SupportsWrite
2+
from collections.abc import Iterator, Mapping
3+
from typing_extensions import Final
4+
5+
BIG_ENDIAN_PLATFORM: bool
6+
P: Final = 1
7+
INET_PTON: Final = 1
8+
Z: Final = 2
9+
ZEROFILL: Final = 2
10+
N: Final = 4
11+
NOHOST: Final = 4
12+
13+
class AddrFormatError(Exception): ...
14+
class AddrConversionError(Exception): ...
15+
class NotRegisteredError(Exception): ...
16+
17+
def num_bits(int_val: int) -> int: ...
18+
19+
class Subscriber:
20+
def update(self, data: Incomplete) -> None: ...
21+
22+
class PrettyPrinter(Subscriber):
23+
fh: SupportsWrite[str]
24+
write_eol: bool
25+
def __init__(self, fh: SupportsWrite[str] = ..., write_eol: bool = ...) -> None: ...
26+
def update(self, data: object) -> None: ...
27+
28+
class Publisher:
29+
subscribers: list[Subscriber]
30+
def __init__(self) -> None: ...
31+
def attach(self, subscriber: Subscriber) -> None: ...
32+
def detach(self, subscriber: Subscriber) -> None: ...
33+
def notify(self, data: object) -> None: ...
34+
35+
class DictDotLookup:
36+
def __init__(self, d: Mapping[str, object]) -> None: ...
37+
def __getitem__(self, name: str) -> object: ...
38+
def __iter__(self) -> Iterator[str]: ...
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from _typeshed import Self
2+
from typing import ClassVar, SupportsInt, overload
3+
from typing_extensions import Literal, SupportsIndex
4+
5+
from netaddr.core import DictDotLookup
6+
from netaddr.ip import IPAddress
7+
from netaddr.strategy.eui48 import mac_eui48
8+
from netaddr.strategy.eui64 import eui64_base
9+
10+
class BaseIdentifier:
11+
def __init__(self) -> None: ...
12+
def __int__(self) -> int: ...
13+
def __long__(self) -> int: ...
14+
def __oct__(self) -> str: ...
15+
def __hex__(self) -> str: ...
16+
def __index__(self) -> int: ...
17+
18+
class OUI(BaseIdentifier):
19+
records: list[dict[str, object]]
20+
def __init__(self, oui: str | int) -> None: ...
21+
def __eq__(self, other: object) -> bool: ...
22+
def __ne__(self, other: object) -> bool: ...
23+
@property
24+
def reg_count(self) -> int: ...
25+
def registration(self, index: int = ...) -> DictDotLookup: ...
26+
27+
class IAB(BaseIdentifier):
28+
IAB_EUI_VALUES: ClassVar[tuple[int, int]]
29+
@classmethod
30+
def split_iab_mac(cls, eui_int: int, strict: bool = ...) -> tuple[int, int]: ...
31+
record: dict[str, object]
32+
def __init__(self, iab: str | int, strict: bool = ...) -> None: ...
33+
def __eq__(self, other: object) -> bool: ...
34+
def __ne__(self, other: object) -> bool: ...
35+
def registration(self) -> DictDotLookup: ...
36+
37+
class EUI(BaseIdentifier):
38+
def __init__(
39+
self, addr: EUI | int | str, version: int | None = ..., dialect: type[mac_eui48] | type[eui64_base] | None = ...
40+
) -> None: ...
41+
@property
42+
def value(self) -> int: ...
43+
@value.setter
44+
def value(self, value: str | SupportsInt | SupportsIndex) -> None: ...
45+
@property
46+
def dialect(self) -> type[mac_eui48] | type[eui64_base]: ...
47+
@dialect.setter
48+
def dialect(self, value: type[mac_eui48] | type[eui64_base] | None) -> None: ...
49+
@property
50+
def oui(self) -> OUI: ...
51+
@property
52+
def ei(self) -> str: ...
53+
def is_iab(self) -> bool: ...
54+
@property
55+
def iab(self) -> IAB | None: ...
56+
@property
57+
def version(self) -> Literal[48, 64]: ...
58+
@overload
59+
def __getitem__(self, idx: int) -> int: ...
60+
@overload
61+
def __getitem__(self, idx: slice) -> list[int]: ...
62+
@overload
63+
def __getitem__(self, idx: int | slice) -> int | list[int]: ...
64+
def __setitem__(self, idx: int, value: int) -> None: ...
65+
def __hash__(self) -> int: ...
66+
def __eq__(self, other: object) -> bool: ...
67+
def __ne__(self, other: object) -> bool: ...
68+
def __lt__(self, other: EUI | int | str) -> bool: ...
69+
def __le__(self, other: EUI | int | str) -> bool: ...
70+
def __gt__(self, other: EUI | int | str) -> bool: ...
71+
def __ge__(self, other: EUI | int | str) -> bool: ...
72+
def bits(self, word_sep: str | None = ...) -> str: ...
73+
@property
74+
def packed(self) -> bytes: ...
75+
@property
76+
def words(self) -> tuple[int, ...]: ...
77+
@property
78+
def bin(self) -> str: ...
79+
def eui64(self: Self) -> Self: ...
80+
def modified_eui64(self: Self) -> Self: ...
81+
def ipv6(self, prefix: str | SupportsInt | SupportsIndex) -> IPAddress: ...
82+
def ipv6_link_local(self) -> IPAddress: ...
83+
@property
84+
def info(self) -> DictDotLookup: ...
85+
def format(self, dialect: type[mac_eui48] | type[eui64_base] | None = ...) -> str: ...

stubs/netaddr/netaddr/eui/ieee.pyi

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import _csv
2+
from _typeshed import StrOrBytesPath
3+
from collections.abc import Iterable
4+
from typing import Any, BinaryIO, TextIO
5+
from typing_extensions import TypeAlias
6+
7+
from netaddr.core import Publisher, Subscriber
8+
9+
_INDEX: TypeAlias = dict[int, list[tuple[int, int]]]
10+
OUI_INDEX: _INDEX
11+
IAB_INDEX: _INDEX
12+
13+
class FileIndexer(Subscriber):
14+
writer: _csv._writer
15+
def __init__(self, index_file: TextIO | StrOrBytesPath | int) -> None: ...
16+
def update(self, data: Iterable[Any]) -> None: ...
17+
18+
class OUIIndexParser(Publisher):
19+
fh: BinaryIO
20+
def __init__(self, ieee_file: BinaryIO | StrOrBytesPath | int) -> None: ...
21+
def parse(self) -> None: ...
22+
23+
class IABIndexParser(Publisher):
24+
fh: BinaryIO
25+
def __init__(self, ieee_file: BinaryIO | StrOrBytesPath | int) -> None: ...
26+
def parse(self) -> None: ...
27+
28+
def create_index_from_registry(
29+
registry_fh: BinaryIO | StrOrBytesPath | int, index_path: StrOrBytesPath, parser: type[OUIIndexParser] | type[IABIndexParser]
30+
) -> None: ...
31+
def create_indices() -> None: ...
32+
def load_index(index: _INDEX, fp: Iterable[bytes]) -> None: ...
33+
def load_indices() -> None: ...

stubs/netaddr/netaddr/fbsocket.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing_extensions import Literal
2+
3+
AF_INET: Literal[2]
4+
AF_INET6: Literal[10]
5+
6+
def inet_ntoa(packed_ip: bytes) -> str: ...
7+
def inet_ntop(af: int, packed_ip: bytes) -> str: ...
8+
def inet_pton(af: int, ip_string: str) -> str: ...

0 commit comments

Comments
 (0)