From 19cd3ed18b950e084d27d17e5fa7bca8dd108353 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Apr 2022 12:14:10 +0200 Subject: [PATCH 1/8] dateutil.tz.tz: Replace IO with protocols --- stubs/python-dateutil/dateutil/tz/tz.pyi | 29 +++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index 9c13b3bac4a2..cb551a61ae24 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -1,13 +1,11 @@ import datetime -from typing import IO, Any, TypeVar -from typing_extensions import TypeAlias +from types import TracebackType +from typing import Any, Protocol, TypeVar +from typing_extensions import Literal, TypeAlias from ..relativedelta import relativedelta from ._common import _tzinfo as _tzinfo, enfold as enfold, tzname_in_python2 as tzname_in_python2, tzrangebase as tzrangebase -# TODO: _FileObj is used differently in classes below. Some need a byte stream, -# some a str stream. -_FileObj: TypeAlias = str | IO[Any] _DT = TypeVar("_DT", bound=datetime.datetime) ZERO: datetime.timedelta @@ -56,8 +54,16 @@ class _ttinfo: __hash__: Any def __ne__(self, other): ... +class _TZFileReader(Protocol): + # optional attribute: + # name: str + def __enter__(self) -> _TZFileReader: ... + def __exit__(self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None) -> bool | None: ... + def read(self, __size: int) -> bytes: ... + def seek(__target: int, __whence: Literal[1]) -> object: ... + class tzfile(_tzinfo): - def __init__(self, fileobj: _FileObj, filename: str | None = ...) -> None: ... + def __init__(self, fileobj: str | _TZFileReader, filename: str | None = ...) -> None: ... def is_ambiguous(self, dt: datetime.datetime | None, idx: int | None = ...) -> bool: ... def utcoffset(self, dt: datetime.datetime | None) -> datetime.timedelta | None: ... def dst(self, dt: datetime.datetime | None) -> datetime.timedelta | None: ... @@ -84,12 +90,19 @@ class tzrange(tzrangebase): class tzstr(tzrange): hasdst: bool - def __init__(self, s: bytes | _FileObj, posix_offset: bool = ...) -> None: ... + def __init__(self, s: str, posix_offset: bool = ...) -> None: ... @classmethod def instance(cls, name, offset) -> tzoffset: ... +class _ICalReader(Protocol): + # optional attribute: + # read: str + def __enter__(self) -> _ICalReader: ... + def __exit__(self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None) -> bool | None: ... + def read(self) -> str: ... + class tzical: - def __init__(self, fileobj: _FileObj) -> None: ... + def __init__(self, fileobj: str | _ICalReader) -> None: ... def keys(self): ... def get(self, tzid: Any | None = ...): ... From 586ce98c7fac7f0bcc673e711303c747f637d493 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Apr 2022 10:16:15 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/python-dateutil/dateutil/tz/tz.pyi | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index cb551a61ae24..d83c07dfe373 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -58,7 +58,9 @@ class _TZFileReader(Protocol): # optional attribute: # name: str def __enter__(self) -> _TZFileReader: ... - def __exit__(self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None) -> bool | None: ... + def __exit__( + self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None + ) -> bool | None: ... def read(self, __size: int) -> bytes: ... def seek(__target: int, __whence: Literal[1]) -> object: ... @@ -98,7 +100,9 @@ class _ICalReader(Protocol): # optional attribute: # read: str def __enter__(self) -> _ICalReader: ... - def __exit__(self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None) -> bool | None: ... + def __exit__( + self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None + ) -> bool | None: ... def read(self) -> str: ... class tzical: From d1ec2c458c9837ee632a6ef016d763f5681c7c13 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Apr 2022 12:29:29 +0200 Subject: [PATCH 3/8] Remove unused import --- stubs/python-dateutil/dateutil/tz/tz.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index d83c07dfe373..b42a51dd54e0 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -1,7 +1,7 @@ import datetime from types import TracebackType from typing import Any, Protocol, TypeVar -from typing_extensions import Literal, TypeAlias +from typing_extensions import Literal from ..relativedelta import relativedelta from ._common import _tzinfo as _tzinfo, enfold as enfold, tzname_in_python2 as tzname_in_python2, tzrangebase as tzrangebase From a7cb126082d6d1cc6fd5978f5299c38d776af937 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Apr 2022 12:30:38 +0200 Subject: [PATCH 4/8] flake8 fixes --- stubs/python-dateutil/dateutil/tz/tz.pyi | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index b42a51dd54e0..e472435503fc 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -1,4 +1,5 @@ import datetime +from _typeshed import Self from types import TracebackType from typing import Any, Protocol, TypeVar from typing_extensions import Literal @@ -57,10 +58,8 @@ class _ttinfo: class _TZFileReader(Protocol): # optional attribute: # name: str - def __enter__(self) -> _TZFileReader: ... - def __exit__( - self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None - ) -> bool | None: ... + def __enter__(self: Self) -> Self: ... + def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> bool | None: ... def read(self, __size: int) -> bytes: ... def seek(__target: int, __whence: Literal[1]) -> object: ... @@ -99,10 +98,8 @@ class tzstr(tzrange): class _ICalReader(Protocol): # optional attribute: # read: str - def __enter__(self) -> _ICalReader: ... - def __exit__( - self, exc_type: type[BaseException] | None, exc_value: Exception | None, traceback: TracebackType | None - ) -> bool | None: ... + def __enter__(self: Self) -> Self: ... + def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> bool | None: ... def read(self) -> str: ... class tzical: From 239dc3be7a8763d641ca1c6cc583a09c77edfc78 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Apr 2022 12:31:00 +0200 Subject: [PATCH 5/8] Add missing self argument --- stubs/python-dateutil/dateutil/tz/tz.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index e472435503fc..583f2c7283d8 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -61,7 +61,7 @@ class _TZFileReader(Protocol): def __enter__(self: Self) -> Self: ... def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> bool | None: ... def read(self, __size: int) -> bytes: ... - def seek(__target: int, __whence: Literal[1]) -> object: ... + def seek(self, __target: int, __whence: Literal[1]) -> object: ... class tzfile(_tzinfo): def __init__(self, fileobj: str | _TZFileReader, filename: str | None = ...) -> None: ... From 7b058b296fb5a90282a0a628a1d71ea963276569 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Apr 2022 10:34:16 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/python-dateutil/dateutil/tz/tz.pyi | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index 583f2c7283d8..7e7e5127f0dc 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -59,7 +59,9 @@ class _TZFileReader(Protocol): # optional attribute: # name: str def __enter__(self: Self) -> Self: ... - def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> bool | None: ... + def __exit__( + self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None + ) -> bool | None: ... def read(self, __size: int) -> bytes: ... def seek(self, __target: int, __whence: Literal[1]) -> object: ... @@ -99,7 +101,9 @@ class _ICalReader(Protocol): # optional attribute: # read: str def __enter__(self: Self) -> Self: ... - def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> bool | None: ... + def __exit__( + self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None + ) -> bool | None: ... def read(self) -> str: ... class tzical: From 93fc4186059a4057a89eb8854351a6c2b49ec297 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Apr 2022 15:03:11 +0200 Subject: [PATCH 7/8] Update stubs/python-dateutil/dateutil/tz/tz.pyi Co-authored-by: Akuli --- stubs/python-dateutil/dateutil/tz/tz.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index 7e7e5127f0dc..869bd12b1200 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -99,7 +99,7 @@ class tzstr(tzrange): class _ICalReader(Protocol): # optional attribute: - # read: str + # name: str def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None From 419943ec34d106749866fcf52ddd64f73f5fbd0f Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 27 Apr 2022 15:06:15 +0200 Subject: [PATCH 8/8] Remove unnecessary context methods --- stubs/python-dateutil/dateutil/tz/tz.pyi | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/stubs/python-dateutil/dateutil/tz/tz.pyi b/stubs/python-dateutil/dateutil/tz/tz.pyi index 869bd12b1200..e67a0f46ae03 100644 --- a/stubs/python-dateutil/dateutil/tz/tz.pyi +++ b/stubs/python-dateutil/dateutil/tz/tz.pyi @@ -1,6 +1,4 @@ import datetime -from _typeshed import Self -from types import TracebackType from typing import Any, Protocol, TypeVar from typing_extensions import Literal @@ -58,10 +56,6 @@ class _ttinfo: class _TZFileReader(Protocol): # optional attribute: # name: str - def __enter__(self: Self) -> Self: ... - def __exit__( - self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None - ) -> bool | None: ... def read(self, __size: int) -> bytes: ... def seek(self, __target: int, __whence: Literal[1]) -> object: ... @@ -100,10 +94,6 @@ class tzstr(tzrange): class _ICalReader(Protocol): # optional attribute: # name: str - def __enter__(self: Self) -> Self: ... - def __exit__( - self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None - ) -> bool | None: ... def read(self) -> str: ... class tzical: