Skip to content

Commit

Permalink
replace typing.* in imports type objects (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemIsmagilov authored and tobixen committed Oct 20, 2024
1 parent 5313d33 commit cfa3af5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 61 deletions.
41 changes: 21 additions & 20 deletions caldav/davclient.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/usr/bin/env python
import logging
import sys
import typing
from types import TracebackType
from typing import Any
from typing import cast
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union
from urllib.parse import unquote

Expand All @@ -25,7 +30,7 @@

from .elements.base import BaseElement

if typing.TYPE_CHECKING:
if TYPE_CHECKING:
pass

if sys.version_info < (3, 9):
Expand Down Expand Up @@ -56,7 +61,7 @@ class DAVResponse:
huge_tree: bool = False

def __init__(
self, response: Response, davclient: typing.Optional["DAVClient"] = None
self, response: Response, davclient: Optional["DAVClient"] = None
) -> None:
self.headers = response.headers
log.debug("response headers: " + str(self.headers))
Expand Down Expand Up @@ -147,9 +152,7 @@ def __init__(
def raw(self) -> str:
## TODO: this should not really be needed?
if not hasattr(self, "_raw"):
self._raw = etree.tostring(
typing.cast(_Element, self.tree), pretty_print=True
)
self._raw = etree.tostring(cast(_Element, self.tree), pretty_print=True)
return self._raw.decode()

def _strip_to_multistatus(self):
Expand Down Expand Up @@ -198,17 +201,15 @@ def validate_status(self, status: str) -> None:
):
raise error.ResponseError(status)

def _parse_response(
self, response
) -> typing.Tuple[str, typing.List[_Element], typing.Optional[typing.Any]]:
def _parse_response(self, response) -> Tuple[str, List[_Element], Optional[Any]]:
"""
One response should contain one or zero status children, one
href tag and zero or more propstats. Find them, assert there
isn't more in the response and return those three fields
"""
status = None
href: typing.Optional[str] = None
propstats: typing.List[_Element] = []
href: Optional[str] = None
propstats: List[_Element] = []
error.assert_(response.tag == dav.Response.tag)
for elem in response:
if elem.tag == dav.Status.tag:
Expand All @@ -224,9 +225,9 @@ def _parse_response(
else:
error.assert_(False)
error.assert_(href)
return (typing.cast(str, href), propstats, status)
return (cast(str, href), propstats, status)

def find_objects_and_props(self) -> typing.Dict[str, typing.Dict[str, _Element]]:
def find_objects_and_props(self) -> Dict[str, Dict[str, _Element]]:
"""Check the response from the server, check that it is on an expected format,
find hrefs and props from it and check statuses delivered.
Expand All @@ -236,7 +237,7 @@ def find_objects_and_props(self) -> typing.Dict[str, typing.Dict[str, _Element]]
self.sync_token will be populated if found, self.objects will be populated.
"""
self.objects: typing.Dict[str, typing.Dict[str, _Element]] = {}
self.objects: Dict[str, Dict[str, _Element]] = {}

if "Schedule-Tag" in self.headers:
self.schedule_tag = self.headers["Schedule-Tag"]
Expand Down Expand Up @@ -311,9 +312,9 @@ def _expand_simple_prop(
def expand_simple_props(
self,
props: Iterable[BaseElement] = None,
multi_value_props: Iterable[typing.Any] = None,
multi_value_props: Iterable[Any] = None,
xpath: Optional[str] = None,
) -> typing.Dict[str, typing.Dict[str, str]]:
) -> Dict[str, Dict[str, str]]:
"""
The find_objects_and_props() will stop at the xml element
below the prop tag. This method will expand those props into
Expand Down Expand Up @@ -344,7 +345,7 @@ def expand_simple_props(
prop.tag, props_found, xpath=xpath, multi_value_allowed=True
)
# _Element objects in self.objects are parsed to str, thus the need to cast the return
return typing.cast(typing.Dict[str, typing.Dict[str, str]], self.objects)
return cast(Dict[str, Dict[str, str]], self.objects)


class DAVClient:
Expand All @@ -369,8 +370,8 @@ def __init__(
auth: Optional[AuthBase] = None,
timeout: Optional[int] = None,
ssl_verify_cert: Union[bool, str] = True,
ssl_cert: Union[str, typing.Tuple[str, str], None] = None,
headers: typing.Dict[str, str] = None,
ssl_cert: Union[str, Tuple[str, str], None] = None,
headers: Dict[str, str] = None,
huge_tree: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -444,7 +445,7 @@ def __enter__(self) -> Self:

def __exit__(
self,
exc_type: Optional[typing.Type[BaseException]],
exc_type: Optional[BaseException],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
Expand Down
3 changes: 1 addition & 2 deletions caldav/elements/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
import sys
import typing
from typing import ClassVar
from typing import List
from typing import Optional
Expand All @@ -26,7 +25,7 @@ class BaseElement:
children: Optional[List[Self]] = None
tag: ClassVar[Optional[str]] = None
value: Optional[str] = None
attributes: typing.Optional[dict] = None
attributes: Optional[dict] = None
caldav_class = None

def __init__(
Expand Down
6 changes: 2 additions & 4 deletions caldav/lib/error.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
import logging
import typing
from collections import defaultdict
from typing import Dict
from typing import Optional

from caldav import __version__
Expand Down Expand Up @@ -118,9 +118,7 @@ class ResponseError(DAVError):
pass


exception_by_method: typing.Dict[str, typing.Type[DAVError]] = defaultdict(
lambda: DAVError
)
exception_by_method: Dict[str, DAVError] = defaultdict(lambda: DAVError)
for method in (
"delete",
"put",
Expand Down
4 changes: 2 additions & 2 deletions caldav/lib/namespace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
import typing
from typing import Any
from typing import Dict
from typing import Optional

Expand All @@ -13,7 +13,7 @@
## calendar-color and calendar-order properties. However, those
## attributes aren't described anywhere, and the I-URL even gives a
## 404! I don't want to ship it in the namespace list of every request.
nsmap2: Dict[str, typing.Any] = nsmap.copy()
nsmap2: Dict[str, Any] = nsmap.copy()
nsmap2["I"] = ("http://apple.com/ns/ical/",)


Expand Down
14 changes: 7 additions & 7 deletions caldav/lib/url.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python
import sys
import typing
import urllib.parse
from typing import Any
from typing import cast
from typing import Optional
from typing import Union
from urllib.parse import ParseResult
from urllib.parse import quote
Expand Down Expand Up @@ -51,7 +53,7 @@ class URL:

def __init__(self, url: Union[str, ParseResult, SplitResult]) -> None:
if isinstance(url, ParseResult) or isinstance(url, SplitResult):
self.url_parsed: typing.Optional[Union[ParseResult, SplitResult]] = url
self.url_parsed: Optional[Union[ParseResult, SplitResult]] = url
self.url_raw = None
else:
self.url_raw = url
Expand Down Expand Up @@ -95,9 +97,7 @@ def __getattr__(self, attr: str):
if "url_parsed" not in vars(self):
raise AttributeError
if self.url_parsed is None:
self.url_parsed = typing.cast(
urllib.parse.ParseResult, urlparse(self.url_raw)
)
self.url_parsed = cast(urllib.parse.ParseResult, urlparse(self.url_raw))
if hasattr(self.url_parsed, attr):
return getattr(self.url_parsed, attr)
else:
Expand Down Expand Up @@ -151,7 +151,7 @@ def canonical(self) -> "URL":
"""
url = self.unauth()

arr = list(typing.cast(urllib.parse.ParseResult, self.url_parsed))
arr = list(cast(urllib.parse.ParseResult, self.url_parsed))
## quoting path and removing double slashes
arr[2] = quote(unquote(url.path.replace("//", "/")))
## sensible defaults
Expand All @@ -172,7 +172,7 @@ def canonical(self) -> "URL":

return url

def join(self, path: typing.Any) -> "URL":
def join(self, path: Any) -> "URL":
"""
assumes this object is the base URL or base path. If the path
is relative, it should be appended to the base. If the path
Expand Down
Loading

0 comments on commit cfa3af5

Please sign in to comment.