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

DISPATCH-2321 Add mypy check for the Python code #46

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions python/qpid_dispatch/management/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,27 @@ def __init__(self, description): ManagementError.__init__(self, status, descript
return Error


class BadRequestStatus(_error_class(BAD_REQUEST)):
class BadRequestStatus(_error_class(BAD_REQUEST)): # type: ignore[misc] # Unsupported dynamic base class "_error_class"
pass


class UnauthorizedStatus(_error_class(UNAUTHORIZED)):
class UnauthorizedStatus(_error_class(UNAUTHORIZED)): # type: ignore[misc]
pass


class ForbiddenStatus(_error_class(FORBIDDEN)):
class ForbiddenStatus(_error_class(FORBIDDEN)): # type: ignore[misc]
pass


class NotFoundStatus(_error_class(NOT_FOUND)):
class NotFoundStatus(_error_class(NOT_FOUND)): # type: ignore[misc]
pass


class InternalServerErrorStatus(_error_class(INTERNAL_SERVER_ERROR)):
class InternalServerErrorStatus(_error_class(INTERNAL_SERVER_ERROR)): # type: ignore[misc]
pass


class NotImplementedStatus(_error_class(NOT_IMPLEMENTED)):
class NotImplementedStatus(_error_class(NOT_IMPLEMENTED)): # type: ignore[misc]
pass


Expand Down
68 changes: 68 additions & 0 deletions python/qpid_dispatch_internal/dispatch.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License
#

"""Type stubs for objects implemented in the C extension module"""

import ctypes
from typing import List


class QdDll(ctypes.PyDLL):
def __init__(self, handle):
...

def _prototype(self, f, restype, argtypes, check=True):
...

def function(self, fname, restype, argtypes, check=True):
...


FORBIDDEN: List[str]

LOG_TRACE: int
LOG_DEBUG: int
LOG_INFO: int
LOG_NOTICE: int
LOG_WARNING: int
LOG_ERROR: int
LOG_CRITICAL: int
LOG_STACK_LIMIT: int

TREATMENT_MULTICAST_FLOOD: int
TREATMENT_MULTICAST_ONCE: int
TREATMENT_ANYCAST_CLOSEST: int
TREATMENT_ANYCAST_BALANCED: int
TREATMENT_LINK_BALANCED: int


class LogAdapter:
def __init__(self, mod_name):
...

def log(self, level, text):
...


class IoAdapter:
def __init__(self, handler, address, global_address=False):
...

def send(self, address, properties, application_properties, body, correlation_id=None):
...
5 changes: 2 additions & 3 deletions tests/TCP_echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def split_chunk_for_display(raw_bytes):
class TcpEchoServer:

def __init__(self, prefix="ECHO_SERVER", port: Union[str, int] = "0", echo_count=0, timeout=0.0, logger=None,
conn_stall=0.0, close_on_conn=False, close_on_data=False):
conn_stall=0.0, close_on_conn=False, close_on_data=False) -> None:
"""
Start echo server in separate thread

Expand All @@ -92,9 +92,8 @@ def __init__(self, prefix="ECHO_SERVER", port: Union[str, int] = "0", echo_count
:param echo_count: exit after echoing this many bytes
:param timeout: exit after this many seconds
:param logger: Logger() object
:return:
"""
self.sock = None
self.sock: socket.socket
self.prefix = prefix
self.port = int(port)
self.echo_count = echo_count
Expand Down
21 changes: 15 additions & 6 deletions tests/http2_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

from quart import Quart, request
try:
from quart.static import send_file
from quart.static import send_file # type: ignore[attr-defined]
except ImportError:
from quart.helpers import send_file
from quart.helpers import send_file # type: ignore[attr-defined, no-redef] # mypy#1153

try:
from quart.exceptions import HTTPStatusException
from quart.exceptions import HTTPStatusException # type: ignore[attr-defined]
except ImportError:
from werkzeug.exceptions import InternalServerError as HTTPStatusException
from werkzeug.exceptions import InternalServerError as HTTPStatusException # type: ignore[no-redef] # mypy#1153

app = Quart(__name__)

Expand Down Expand Up @@ -117,5 +117,14 @@ async def process_upload_data():
return "Success!"


#app.run(port=5000, certfile='cert.pem', keyfile='key.pem')
app.run(port=os.getenv('SERVER_LISTEN_PORT'))
def main():
port = os.getenv('SERVER_LISTEN_PORT')
if port is None:
raise RuntimeError("Environment variable `SERVER_LISTEN_PORT` is not set.")

# app.run(port=5000, certfile='cert.pem', keyfile='key.pem')
app.run(port=int(port))


if __name__ == '__main__':
main()
43 changes: 26 additions & 17 deletions tests/http2_slow_q2_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,29 @@ def handle(sock):
sock.sendall(data_to_send)


signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGQUIT, receive_signal)
signal.signal(signal.SIGILL, receive_signal)
signal.signal(signal.SIGTERM, receive_signal)

sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', int(os.getenv('SERVER_LISTEN_PORT'))))
sock.listen(5)

while True:
# The accept method blocks until someone attempts to connect to our TCP
# port: when they do, it returns a tuple: the first element is a new
# socket object, the second element is a tuple of the address the new
# connection is from
handle(sock.accept()[0])
def main():
signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGQUIT, receive_signal)
signal.signal(signal.SIGILL, receive_signal)
signal.signal(signal.SIGTERM, receive_signal)

port = os.getenv('SERVER_LISTEN_PORT')
if port is None:
raise RuntimeError("Environment variable `SERVER_LISTEN_PORT` is not set.")

sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', int(port)))
sock.listen(5)

while True:
# The accept method blocks until someone attempts to connect to our TCP
# port: when they do, it returns a tuple: the first element is a new
# socket object, the second element is a tuple of the address the new
# connection is from
handle(sock.accept()[0])


if __name__ == '__main__':
main()
43 changes: 26 additions & 17 deletions tests/hyperh2_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,29 @@ def handle(sock):
sock.sendall(data_to_send)


signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGQUIT, receive_signal)
signal.signal(signal.SIGILL, receive_signal)
signal.signal(signal.SIGTERM, receive_signal)

sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', int(os.getenv('SERVER_LISTEN_PORT'))))
sock.listen(5)

while True:
# The accept method blocks until someone attempts to connect to our TCP
# port: when they do, it returns a tuple: the first element is a new
# socket object, the second element is a tuple of the address the new
# connection is from
handle(sock.accept()[0])
def main():
signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGQUIT, receive_signal)
signal.signal(signal.SIGILL, receive_signal)
signal.signal(signal.SIGTERM, receive_signal)

port = os.getenv('SERVER_LISTEN_PORT')
if port is None:
raise RuntimeError("Environment variable `SERVER_LISTEN_PORT` is not set.")

sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', int(port)))
sock.listen(5)

while True:
# The accept method blocks until someone attempts to connect to our TCP
# port: when they do, it returns a tuple: the first element is a new
# socket object, the second element is a tuple of the address the new
# connection is from
handle(sock.accept()[0])


if __name__ == '__main__':
main()
2 changes: 0 additions & 2 deletions tests/run_system_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@
all_tests.addTest(tests)
result = unittest.TextTestRunner(verbosity=2).run(all_tests)
sys.exit(not result.wasSuccessful())

sys.argv = ['unittest', '-v'] + tests
jiridanek marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions tests/system_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
- Sundry other tools.
"""

from typing import Callable

import errno
import sys
import time
Expand Down Expand Up @@ -126,7 +128,7 @@ def retry_delay(deadline, delay, max_delay):
TIMEOUT = float(os.environ.get("QPID_SYSTEM_TEST_TIMEOUT", 60))


def retry(function, timeout=TIMEOUT, delay=.001, max_delay=1):
def retry(function: Callable[[], bool], timeout: float = TIMEOUT, delay: float = .001, max_delay: float = 1):
"""Call function until it returns a true value or timeout expires.
Double the delay for each retry up to max_delay.
Returns what function returns or None if timeout expires.
Expand Down Expand Up @@ -382,7 +384,7 @@ def wait_ports(self, **retry_kwargs):
class Qdrouterd(Process):
"""Run a Qpid Dispatch Router Daemon"""

class Config(list, Config):
class Config(list, Config): # type: ignore[misc] # Cannot resolve name "Config" (possible cyclic definition) # mypy#10958
"""
A router configuration.

Expand Down
4 changes: 1 addition & 3 deletions tests/system_tests_edge_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,7 @@ def __init__(self, receiver1_host, receiver2_host, receiver3_host,

def on_released(self, event):
self.n_released += 1
self.send_test_message()
jiridanek marked this conversation as resolved.
Show resolved Hide resolved

def timeout(self):
if self.dup_msg:
Expand Down Expand Up @@ -2144,9 +2145,6 @@ def on_message(self, event):
self.receiver3_conn.close()
self.sender_conn.close()

def on_released(self, event):
self.send_test_message()

def run(self):
Container(self).run()

Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#


from proton import Message, Timeout
from proton import Message
jiridanek marked this conversation as resolved.
Show resolved Hide resolved
from proton.handlers import MessagingHandler
from proton.reactor import Container

Expand Down
4 changes: 0 additions & 4 deletions tests/system_tests_sasl_plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,6 @@ def setUpClass(cls):

cls.routers[1].wait_router_connected('QDR.X')

@staticmethod
def ssl_file(name):
jiridanek marked this conversation as resolved.
Show resolved Hide resolved
return os.path.join(DIR, 'ssl_certs', name)

def common_asserts(self, results):
search = "QDR.X"
found = False
Expand Down
10 changes: 3 additions & 7 deletions tests/system_tests_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class RouterTestSslClient(RouterTestSslBase):
p = Popen(['openssl', 'version'], stdout=PIPE, universal_newlines=True)
openssl_out = p.communicate()[0]
m = re.search(r'[0-9]+\.[0-9]+\.[0-9]+', openssl_out)
assert m is not None
OPENSSL_OUT_VER = m.group(0)
OPENSSL_VER_1_1_GT = StrictVersion(OPENSSL_OUT_VER) >= StrictVersion('1.1')
print("OpenSSL Version found = %s" % OPENSSL_OUT_VER)
Expand All @@ -132,13 +133,8 @@ class RouterTestSslClient(RouterTestSslBase):
OPENSSL_ALLOW_TLSV1_3 = False

# Test if OpenSSL has TLSv1_3
OPENSSL_HAS_TLSV1_3 = False
if OPENSSL_VER_1_1_GT:
try:
_ = ssl.TLSVersion.TLSv1_3
OPENSSL_HAS_TLSV1_3 = True
except AttributeError:
pass
# (see https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks for mypy considerations)
OPENSSL_HAS_TLSV1_3 = OPENSSL_VER_1_1_GT and sys.version_info >= (3, 7) and ssl.HAS_TLSv1_3

# Test if Proton supports TLSv1_3
try:
Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import time

from proton import Message, Timeout
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container

Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests_topology_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import unittest

from proton import Message, Timeout
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container

Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests_topology_disposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from subprocess import PIPE, STDOUT

import proton
from proton import Message, Timeout
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container
from qpid_dispatch_internal.compat import UNICODE
Expand Down
2 changes: 1 addition & 1 deletion tests/system_tests_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
try:
import websockets
except ImportError:
websockets = None
websockets = None # type: ignore[assignment] # expression has type "None", variable has type Module

from system_test import Qdrouterd
from system_test import main_module, TestCase, Process
Expand Down
4 changes: 2 additions & 2 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def mock_error(self, message):
raise ValueError(message)


argparse.ArgumentParser.error = mock_error
argparse.ArgumentParser.error = mock_error # type: ignore[assignment] # Cannot assign to a method

# Since BusManager file is definded in tools/qdmanage.in -> tools/qdmanage
# Since BusManager file is defined in tools/qdmanage.in -> tools/qdmanage
# otherwise it could be just imported


Expand Down
Loading