-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix(typing): Typing fixes #18
base: main
Are you sure you want to change the base?
Conversation
- Use `Any` instead of `any` - Import `__future__.annotations` for compatibility - Use `|` operator instead of `Union[]` - Only use `Any` if `Any` is used in the union - Fix `super().__init__()` arguments - None check if required - Use `ABC` base class to `AbstractHandlerBase`, use `@abstractmethod` to the class's methods
@@ -501,7 +510,7 @@ def make_cache(self) -> None: | |||
cursor[method] = EndPoint(method, rt, path, function, auth, args, bool(paths), docs) | |||
self.count += 1 | |||
|
|||
def get_endpoint(self, method: str, path: str, params: Optional[dict] = None) -> Optional[EndPoint]: | |||
def get_endpoint(self, method: str, path: str, params: dict = {}) -> Optional[EndPoint]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引数のデフォルト値が mutable です。
path = parse.urlparse(self.request.path) | ||
queries = dict(parse.parse_qsl(path.query)) | ||
|
||
if self.request.method in ["GET", "HEAD", "TRACE", "OPTIONS"]: | ||
|
||
self.call_handler(path.path, {}, queries) | ||
else: | ||
if "Content-Type" in self.request.headers: | ||
if self.request.headers is not None and "Content-Type" in self.request.headers: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.request.headers
がNoneになることはありません。
@@ -81,34 +86,43 @@ | |||
header_limit = 100 | |||
|
|||
|
|||
class AbstractHandlerBase(StreamRequestHandler): | |||
class AbstractHandlerBase(ABC, StreamRequestHandler): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Abstractである必要がありません
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このクラスからの継承を止めて、子クラスで維持させた方がいいかもしれません。
全部の子クラスで使用されている関数があればabcを使用します。
src/endpoint.py
Outdated
@@ -1,5 +1,7 @@ | |||
from __future__ import annotations | |||
from typing import Union, Optional | |||
|
|||
from typing import Optional, Any, Literal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python3.7での互換性を保てません。
@@ -277,7 +292,7 @@ def _header(self, data: str) -> None: | |||
if len(kv) != 2: | |||
raise ParseException("MALFORMED_HEADER") | |||
|
|||
self._response.headers.add(*kv) | |||
self._response.headers.add(*kv) if self._response.headers is not None else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._response.headers
が None になることはありません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noneチェックが必要ない型にするには__init__
からNone定義をしている箇所を削除する必要があります。
self.multiple = True | ||
elif req.headers["Connection"] == "close": | ||
self.multiple = False | ||
if req.headers is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req.headers
がNoneになることはありません。
|
||
def handle_switch(self): | ||
try: | ||
if self.request is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.request
がNoneになることはありません。
@@ -149,18 +160,24 @@ def log_request(self, **kwargs): | |||
if not no_req_log: | |||
self.logger.info(get_log_name(), '%s -- %s %s -- "%s %s"' % | |||
(kwargs["client"], kwargs["code"], "" if kwargs["message"] is None else kwargs["message"], | |||
self.request.method, kwargs["path"])) | |||
self.request.method if self.request is not None else "<no method>", kwargs["path"])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.request
がNoneになることはありません。
ep = endpoint.loader.get_endpoint(self.request.method, path, path_param) | ||
ep: Optional[endpoint.EndPoint] = None | ||
|
||
if self.request is not None and self.request.method is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.request
やself.request.method
Noneになることはありません。
レビューを承認する前に入念に確認してください。見落としがあるかもしれません。
any
の代わりにAny
を使用-
any()
を指定していることになっています。__future__.annotations
for compatibility- 互換性維持のため。
|
operator instead ofUnion[]
-
__future__.annotations
が適用されている状態でこの記法ができます。Any
ifAny
is used in the union-
Any
はすべての型を通すため、Union
やOptional
である意味がありません。Any
を使用したくない場合は代わりの型をレビューでリクエストしてください。super().__init__()
arguments- 一部
super().__init__()
の引数が間違っていた箇所があります。-
None
がチェックされていない箇所にNoneチェックを入れました。ABC
base class toAbstractHandlerBase
, use@abstractmethod
to the class's methods-
ABC
が使用されていないため入れましたが、なにか理由があれば修正します。以下の推奨事項があります。修正願います。
TypedDict
を使用したdict
のキーの固定tuple[]
の型の設定Any
が使用されている箇所の代替となる型の指定Authorization
ヘッダーのチェック部分でのNoneチェックの実装