Skip to content

Commit

Permalink
add type annotations to __getitem__ variables
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Mar 16, 2024
1 parent 00fe8f2 commit 5b9b0cc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
20 changes: 17 additions & 3 deletions tests/test_types_objects.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
- case: fail_on_missing_method
regex: yes
main: |
from zammadoo import Client
client = Client("<url>", http_token="<token>")
client.tickets(1).missing_method()
out: |
main:4: error: "None" not callable \[misc\]
main:4: error: "bool" not callable \[operator\]
main:4: error: "int" not callable \[operator\]
main:4: error: "float" not callable \[operator\]
main:4: error: "str" not callable \[operator\]
main:4: error: "[Ll]ist\[JsonType\]" not callable \[operator\]
main:4: error: "[Dd]ict\[str, JsonType\]" not callable \[operator\]
main:4: error: "datetime" not callable \[operator\]
- case: client_attributes_are_of_proper_type
skip: sys.implementation.name == "pypy"
main: |
from zammadoo import Client
client = Client("<url>", http_token="<token>")
client.tickets(1).missing_method() # E: "object" not callable [operator]
client = Client("<url>", http_token="<token>")
reveal_type(client.groups) # N: Revealed type is "zammadoo.groups.Groups"
reveal_type(
Expand Down
19 changes: 10 additions & 9 deletions zammadoo/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,32 +170,33 @@ class Article(Resource):

@property
def created_by(self) -> "User":
uid = self["created_by_id"]
uid: int = self["created_by_id"]
return self.parent.client.users(uid)

@property
def origin_by(self) -> Optional["User"]:
oid = self["origin_by_id"]
oid: int = self["origin_by_id"]
return self.parent.client.users(oid)

@property
def updated_by(self) -> "User":
uid = self["updated_by_id"]
uid: int = self["updated_by_id"]
return self.parent.client.users(uid)

@property
def ticket(self) -> "Ticket":
return self.parent.client.tickets(self["ticket_id"])
tid: int = self["ticket_id"]
return self.parent.client.tickets(tid)

@property
def attachments(self) -> List[Attachment]:
"""A list of the articles attachments."""
attachment_list = []
client = self.parent.client
for info in self["attachments"]:
url = f"{client.url}/ticket_attachment/{self['ticket_id']}/{self.id}/{info['id']}"
attachment_list.append(Attachment(client, url, info))
return attachment_list
attachment_url = f"{client.url}/ticket_attachment/{self['ticket_id']}/{self.id}"
return [
Attachment(client, f"{attachment_url}/{info['id']}", info)
for info in self["attachments"]
]


class Articles(CreatableT[Article], ResourcesT[Article]):
Expand Down
4 changes: 2 additions & 2 deletions zammadoo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ class MutableResource(Resource):

@property
def created_by(self) -> "User":
uid = self["created_by_id"]
uid: int = self["created_by_id"]
return self.parent.client.users(uid)

@property
def updated_by(self) -> "User":
uid = self["updated_by_id"]
uid: int = self["updated_by_id"]
return self.parent.client.users(uid)

def update(self: _T_co, **kwargs) -> _T_co:
Expand Down
24 changes: 12 additions & 12 deletions zammadoo/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class State(NamedResource):

@property
def next_state(self) -> Optional["State"]:
sid = self["next_state_id"]
return sid and self.parent.client.ticket_states(sid)
sid: Optional[int] = self["next_state_id"]
return None if sid is None else self.parent.client.ticket_states(sid)

@property
def state_type(self) -> "State":
sid = self["state_type_id"]
sid: int = self["state_type_id"]
return self.parent.client.ticket_states(sid)


Expand Down Expand Up @@ -94,7 +94,6 @@ class Ticket(MutableResource):
EXPANDED_ATTRIBUTES = "article_ids", "create_article_sender", "create_article_type"

article_count: Optional[int] #:
article_ids: List[int] #:
create_article_sender: str #:
create_article_type: str #:
note: Optional[str] #:
Expand All @@ -104,36 +103,36 @@ class Ticket(MutableResource):

@property
def customer(self) -> "User":
uid = self["customer_id"]
uid: int = self["customer_id"]
return self.parent.client.users(uid)

@property
def group(self) -> "Group":
gid = self["group_id"]
gid: int = self["group_id"]
return self.parent.client.groups(gid)

@property
def organization(self) -> Optional["Organization"]:
oid = self["organization_id"]
return oid and self.parent.client.organizations(oid)
oid: Optional[int] = self["organization_id"]
return None if oid is None else self.parent.client.organizations(oid)

@property
def owner(self) -> "User":
"""
.. note::
unassigned tickets will be represented by User with id=1
"""
uid = self["owner_id"]
uid: int = self["owner_id"]
return self.parent.client.users(uid)

@property
def priority(self) -> Priority:
pid = self["priority_id"]
pid: int = self["priority_id"]
return self.parent.client.ticket_priorities(pid)

@property
def state(self) -> State:
sid = self["state_id"]
sid: int = self["state_id"]
return self.parent.client.ticket_states(sid)

@property
Expand All @@ -142,7 +141,8 @@ def articles(self) -> List["Article"]:
all articles related to the ticket as sent by ``/ticket_articles/by_ticket/{ticket id}``
"""
articles = self.parent.client.ticket_articles
return [articles(aid) for aid in sorted(self.article_ids)]
article_ids: List[int] = self["article_ids"]
return [articles(aid) for aid in sorted(article_ids)]

@property
def time_accountings(self) -> List[TimeAccounting]:
Expand Down
12 changes: 6 additions & 6 deletions zammadoo/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ def groups(self) -> List[Group]:

@property
def last_login(self) -> Optional[datetime]:
last_login = self["last_login"]
return last_login and fromisoformat(last_login)
last_login: Optional[str] = self["last_login"]
return None if last_login is None else fromisoformat(last_login)

@property
def organization(self) -> Optional["Organization"]:
oid = self["organization_id"]
return oid and self.parent.client.organizations(oid)
oid: Optional[int] = self["organization_id"]
return None if oid is None else self.parent.client.organizations(oid)

@property
def organizations(self) -> List["Organization"]:
Expand All @@ -81,8 +81,8 @@ def organizations(self) -> List["Organization"]:

@property
def out_of_office_replacement(self) -> Optional["User"]:
uid = self["out_of_office_replacement_id"]
return uid and self.parent.client.users(uid)
uid: Optional[int] = self["out_of_office_replacement_id"]
return None if uid is None else self.parent.client.users(uid)

@property
def roles(self) -> List["Role"]:
Expand Down
4 changes: 2 additions & 2 deletions zammadoo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def __init__(
self._info = info or {}
self._frozen = True

def __getattr__(self, name: str) -> object:
info = self._info
def __getattr__(self, name: str) -> Union["JsonType", datetime]:
info: Dict[str, "JsonType"] = self._info
key = "from" if name == "from_" else name
if key in info:
if not key.endswith("_at"):
Expand Down

0 comments on commit 5b9b0cc

Please sign in to comment.