0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-30 08:24:11 +01:00

Pull in netaddr type hints. (#15231)

And fix any issues from having those type hints.
This commit is contained in:
Patrick Cloke 2023-03-09 07:09:49 -05:00 committed by GitHub
parent be4ea209e8
commit e7c3832ba6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 12 deletions

1
changelog.d/15231.misc Normal file
View file

@ -0,0 +1 @@
Improve type hints.

View file

@ -74,11 +74,6 @@ ignore_missing_imports = True
[mypy-msgpack] [mypy-msgpack]
ignore_missing_imports = True ignore_missing_imports = True
# Note: WIP stubs available at
# https://github.com/microsoft/python-type-stubs/tree/64934207f523ad6b611e6cfe039d85d7175d7d0d/netaddr
[mypy-netaddr]
ignore_missing_imports = True
[mypy-parameterized.*] [mypy-parameterized.*]
ignore_missing_imports = True ignore_missing_imports = True

16
poetry.lock generated
View file

@ -1722,7 +1722,7 @@ files = [
cffi = ">=1.4.1" cffi = ">=1.4.1"
[package.extras] [package.extras]
docs = ["sphinx (>=1.6.5)", "sphinx-rtd-theme"] docs = ["sphinx (>=1.6.5)", "sphinx_rtd_theme"]
tests = ["hypothesis (>=3.27.0)", "pytest (>=3.2.1,!=3.3.0)"] tests = ["hypothesis (>=3.27.0)", "pytest (>=3.2.1,!=3.3.0)"]
[[package]] [[package]]
@ -2597,6 +2597,18 @@ files = [
{file = "types_jsonschema-4.17.0.5-py3-none-any.whl", hash = "sha256:79ac8a7763fe728947af90a24168b91621edf7e8425bf3670abd4ea0d4758fba"}, {file = "types_jsonschema-4.17.0.5-py3-none-any.whl", hash = "sha256:79ac8a7763fe728947af90a24168b91621edf7e8425bf3670abd4ea0d4758fba"},
] ]
[[package]]
name = "types-netaddr"
version = "0.8.0.6"
description = "Typing stubs for netaddr"
category = "dev"
optional = false
python-versions = "*"
files = [
{file = "types-netaddr-0.8.0.6.tar.gz", hash = "sha256:e5048640c2412e7ea2d3eb02c94ae1b50442b2c7a50a7c48e957676139cdf19b"},
{file = "types_netaddr-0.8.0.6-py3-none-any.whl", hash = "sha256:d4d40d1ba35430a4e4c929596542cd37e6831f5d08676b33dc84e06e01a840f6"},
]
[[package]] [[package]]
name = "types-opentracing" name = "types-opentracing"
version = "2.4.10.3" version = "2.4.10.3"
@ -2990,4 +3002,4 @@ user-search = ["pyicu"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.7.1" python-versions = "^3.7.1"
content-hash = "7bcffef7b6e6d4b1113222e2ca152b3798c997872789c8a1ea01238f199d56fe" content-hash = "de2c4c8de336593478ce02581a5336afe2544db93ea82f3955b34c3653c29a26"

View file

@ -321,6 +321,7 @@ mypy-zope = "*"
types-bleach = ">=4.1.0" types-bleach = ">=4.1.0"
types-commonmark = ">=0.9.2" types-commonmark = ">=0.9.2"
types-jsonschema = ">=3.2.0" types-jsonschema = ">=3.2.0"
types-netaddr = ">=0.8.0.6"
types-opentracing = ">=2.4.2" types-opentracing = ">=2.4.2"
types-Pillow = ">=8.3.4" types-Pillow = ">=8.3.4"
types-psycopg2 = ">=2.9.9" types-psycopg2 = ">=2.9.9"

View file

@ -268,8 +268,8 @@ class BlacklistingAgentWrapper(Agent):
def __init__( def __init__(
self, self,
agent: IAgent, agent: IAgent,
ip_blacklist: IPSet,
ip_whitelist: Optional[IPSet] = None, ip_whitelist: Optional[IPSet] = None,
ip_blacklist: Optional[IPSet] = None,
): ):
""" """
Args: Args:
@ -291,7 +291,9 @@ class BlacklistingAgentWrapper(Agent):
h = urllib.parse.urlparse(uri.decode("ascii")) h = urllib.parse.urlparse(uri.decode("ascii"))
try: try:
ip_address = IPAddress(h.hostname) # h.hostname is Optional[str], None raises an AddrFormatError, so
# this is safe even though IPAddress requires a str.
ip_address = IPAddress(h.hostname) # type: ignore[arg-type]
except AddrFormatError: except AddrFormatError:
# Not an IP # Not an IP
pass pass
@ -388,8 +390,8 @@ class SimpleHttpClient:
# by the DNS resolution. # by the DNS resolution.
self.agent = BlacklistingAgentWrapper( self.agent = BlacklistingAgentWrapper(
self.agent, self.agent,
ip_whitelist=self._ip_whitelist,
ip_blacklist=self._ip_blacklist, ip_blacklist=self._ip_blacklist,
ip_whitelist=self._ip_whitelist,
) )
async def request( async def request(

View file

@ -87,7 +87,7 @@ class MatrixFederationAgent:
reactor: ISynapseReactor, reactor: ISynapseReactor,
tls_client_options_factory: Optional[FederationPolicyForHTTPS], tls_client_options_factory: Optional[FederationPolicyForHTTPS],
user_agent: bytes, user_agent: bytes,
ip_whitelist: IPSet, ip_whitelist: Optional[IPSet],
ip_blacklist: IPSet, ip_blacklist: IPSet,
_srv_resolver: Optional[SrvResolver] = None, _srv_resolver: Optional[SrvResolver] = None,
_well_known_resolver: Optional[WellKnownResolver] = None, _well_known_resolver: Optional[WellKnownResolver] = None,

View file

@ -210,8 +210,8 @@ class BlacklistingAgentTest(TestCase):
"""Apply the blacklisting agent and ensure it properly blocks connections to particular IPs.""" """Apply the blacklisting agent and ensure it properly blocks connections to particular IPs."""
agent = BlacklistingAgentWrapper( agent = BlacklistingAgentWrapper(
Agent(self.reactor), Agent(self.reactor),
ip_whitelist=self.ip_whitelist,
ip_blacklist=self.ip_blacklist, ip_blacklist=self.ip_blacklist,
ip_whitelist=self.ip_whitelist,
) )
# The unsafe IPs should be rejected. # The unsafe IPs should be rejected.