Decided to try and add type-hint annotation to my project but seems I have quite a few unresolved errors reported by PyCharm, presumably I have quite a few bits wrong. Sorry it is a bit long but code below is just an extraction of those lines which have problems and a few other possibly relevant lines:
1. from wsgiref import simple_server
2.
3. interactive_mode = True
4. if interactive_mode:
5. from collections.abc import Callable
6. from logging import Logger
7.
8. class WsgiController:
9. logger: Logger
10. start_response: Callable[[str, list[tuple[str, str]]], None]
11. oPage: type[Process]
12. def __init__(self, environ: dict[str, str], start_response: Callable[[str, list[tuple[str, str]]], None]):
13. self.environ = environ
14. self.start_response = start_response
15. self.oPage = Process(environ, start_response)
16.
17. httpd = simple_server.make_server("", 8000, WsgiController)
18.
19. class Request:
20. environ: dict[str, str]
21. cookies: dict[str, str]
22.
23. def __init__(self, environ: dict[str, str]) -> None:
24. self.environ = environ
25.
26. def get(self, key: str) -> str | None:
27. return self.environ.get(key)
28.
29. def parse_cookies(self) -> None:
30. _ = self.get("HTTP_COOKIE")
31.
32. def set_cookie(self, name: str, value: str, attributes: None | dict[str, str]=None) -> None:
33. if attributes is None: attributes = {}
34. _ = f"{name}={value};"
35. for _, _ in attributes.items():
36. pass
37.
38. class Response:
39. start_response: Callable[[str, list[tuple[str, str]]], None]
40. headers: list[tuple[str, str]]
41.
42. def __init__(self, start_response) -> None:
43. self.start_response = start_response
44. self.headers = [("", "")]
45.
46. def start_headers(self, status: str, headers: list[tuple[str, str]]) -> None:
47. self.start_response(status, headers + self.headers)
48.
49. def serve_pre_compiled(self, pyc_file: str) -> str:
50. local_dict = {"oRequest": Process.oRequest, "oResponse": self}
51. _ = pyc_file
52. return local_dict["__HTML__"]
53.
54. def serve_new_source(self, source_file: str, module_name: str, pyc_file: str) -> str:
55. import importlib.util
56. import importlib.machinery
57. spec = importlib.util.spec_from_file_location(module_name, source_file)
58. importlib.util.module_from_spec(spec)
59. module = importlib.util.module_from_spec(spec)
60. spec.loader.exec_module(module)
61. _ = self
62. _ = pyc_file
63. return _
64.
65. class Process:
66. oRequest: type[Request]
67. oResponse: type[Response]
68. logger: type[logger]
69.
70. def __init__(self, environ: dict[str, str], start_response: Callable[[str, list[tuple[str, str]]], None]) -> None:
71. Process.oRequest = Request(environ)
72. Process.oResponse = Response(start_response)
73. _ = self.oRequest.get("PATH_INFO")[1:]
74. self.oResponse.start_headers("404 Not Found", [("Content-Type", "text/html; charset=utf-8")])
75. self.oRequest.parse_cookies()
76. _ = self.oResponse.serve_pre_compiled("pyc_file")
77. _ = self.oResponse.serve_new_source("source_file", "module_name", "pyc_file")
78. _ = "- " + self.oResponse.start_response.__self__.status
79. _ = self.oRequest.get("PATH_INFO")[1:]
80. self.oResponse.start_headers('200 OK', [('Content-type', "mime_type" + '; charset=utf-8'), ("Content-Length", str(len("ret_val")))])
81.
Errors reported by PyCharm:
15 Expected type 'type[Process]', got 'Process' instead
17 Expected type '(dict[str, Any], StartResponse) -> Iterable[bytes]', got 'type[WsgiController]' instead
53 Expected type 'str', got 'type[Request] | Self@Response' instead
59 Expected type 'ModuleSpec', got 'ModuleSpec | None' instead
60 Expected type 'ModuleSpec', got 'ModuleSpec | None' instead
72 Expected type 'type[Request]', got 'Request' instead
73 Expected type 'type[Response]', got 'Response' instead
74 Parameter 'key' unfilled
75 Parameter 'headers' unfilled
76 Parameter 'self' unfilled
77 Parameter 'pyc_file' unfilled
78 Parameter 'pyc_file' unfilled
79 Cannot find reference '__self__' in '(str, list) -> None'
80 Parameter 'key' unfilled
81 Parameter 'headers' unfilled
35 Member 'None' of 'dict[str, str] | None' does not have attribute 'items'
60 Member 'None' of 'ModuleSpec | None' does not have attribute 'loader'
60 Member 'None' of 'Loader | None' does not have attribute 'exec_module'
73 Member 'None' of 'str | None' does not have attribute '__getitem__'
79 Member 'None' of 'str | None' does not have attribute '__getitem__'