Skip to content

Commit abbdb75

Browse files
authored
Merge pull request #747 from kellerza/typing
Improve typing #2
2 parents ad4afa8 + bc8c237 commit abbdb75

File tree

12 files changed

+88
-73
lines changed

12 files changed

+88
-73
lines changed

.flake8

-5
This file was deleted.

.pylintrc

-15
This file was deleted.

office365/directory/applications/application.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def remove_certificate(self, thumbprint):
6161
Remove a certificate from an application.
6262
:param str thumbprint: The unique identifier for the password.
6363
"""
64-
raise NotImplemented("")
64+
raise NotImplementedError("")
6565

6666
def add_password(self, display_name):
6767
"""Adds a strong password to an application.

office365/entity.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1+
from typing import TYPE_CHECKING, Optional, TypeVar
2+
3+
from typing_extensions import Self
4+
15
from office365.runtime.client_object import ClientObject
26
from office365.runtime.paths.entity import EntityPath
37
from office365.runtime.paths.resource_path import ResourcePath
48
from office365.runtime.queries.delete_entity import DeleteEntityQuery
59
from office365.runtime.queries.update_entity import UpdateEntityQuery
610

11+
if TYPE_CHECKING:
12+
from office365.graph_client import GraphClient
13+
14+
15+
T = TypeVar("T")
16+
717

8-
class Entity(ClientObject):
18+
class Entity(ClientObject[T]):
919
"""Base entity"""
1020

1121
def update(self):
22+
# type: () -> Self
1223
"""Updates the entity."""
1324
qry = UpdateEntityQuery(self)
1425
self.context.add_query(qry)
1526
return self
1627

1728
def delete_object(self):
29+
# type: () -> Self
1830
"""Deletes the entity."""
1931
qry = DeleteEntityQuery(self)
2032
self.context.add_query(qry)
@@ -23,30 +35,31 @@ def delete_object(self):
2335

2436
@property
2537
def context(self):
26-
"""
27-
:rtype: office365.graph_client.GraphClient
28-
"""
38+
# type: () -> GraphClient
39+
"""Return the Graph Client context."""
2940
return self._context
3041

3142
@property
3243
def entity_type_name(self):
44+
# type: () -> str
3345
if self._entity_type_name is None:
3446
name = type(self).__name__
3547
self._entity_type_name = "microsoft.graph." + name[0].lower() + name[1:]
3648
return self._entity_type_name
3749

3850
@property
3951
def id(self):
40-
"""The unique identifier of the entity.
41-
:rtype: str or None
42-
"""
52+
# type: () -> Optional[str]
53+
"""The unique identifier of the entity."""
4354
return self.properties.get("id", None)
4455

4556
@property
4657
def property_ref_name(self):
58+
# type: () -> str
4759
return "id"
4860

4961
def set_property(self, name, value, persist_changes=True):
62+
# type: (str, T, bool) -> Self
5063
super(Entity, self).set_property(name, value, persist_changes)
5164
if name == self.property_ref_name:
5265
if self._resource_path is None:

office365/entity_collection.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import TypeVar
1+
from typing import TYPE_CHECKING, TypeVar, Any
22

3-
from office365.graph_client import GraphClient
43
from office365.runtime.client_object_collection import ClientObjectCollection
54
from office365.runtime.compat import is_string_type
65
from office365.runtime.paths.item import ItemPath
76
from office365.runtime.paths.resource_path import ResourcePath
87
from office365.runtime.queries.create_entity import CreateEntityQuery
98

9+
if TYPE_CHECKING:
10+
from office365.graph_client import GraphClient
11+
1012
T = TypeVar("T")
1113

1214

office365/graph_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from office365.runtime.auth.token_response import TokenResponse
4949
from office365.runtime.client_runtime_context import ClientRuntimeContext
5050
from office365.runtime.http.http_method import HttpMethod
51+
from office365.runtime.http.request_options import RequestOptions
5152
from office365.runtime.odata.request import ODataRequest
5253
from office365.runtime.odata.v4.batch_request import ODataV4BatchRequest
5354
from office365.runtime.odata.v4.json_format import V4JsonFormat
@@ -68,7 +69,7 @@ class GraphClient(ClientRuntimeContext):
6869
"""Graph Service client"""
6970

7071
def __init__(self, acquire_token_callback):
71-
# type: (Callable[None, dict]) -> None
72+
# type: (Callable[..., dict]) -> None
7273
"""
7374
:param () -> dict acquire_token_callback: Acquire token function
7475
"""

office365/onedrive/driveitems/driveItem.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class DriveItem(BaseItem):
5151
OneDrive and SharePoint are returned as driveItem resources"""
5252

5353
def get_by_path(self, url_path):
54+
# type: (str) -> DriveItem
5455
"""
5556
Retrieve DriveItem by server relative path
5657
@@ -236,8 +237,8 @@ def upload_file(self, path_or_file):
236237

237238
def get_content(self, format_name=None):
238239
"""
239-
Download the contents of the primary stream (file) of a DriveItem. O
240-
nly driveItems with the file property can be downloaded.
240+
Download the contents of the primary stream (file) of a DriveItem.
241+
Only driveItems with the file property can be downloaded.
241242
242243
:type format_name: str or None
243244
"""

office365/runtime/client_object.py

+30-35
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
from __future__ import annotations
2+
13
import datetime
2-
from typing import TypeVar
4+
from typing import TYPE_CHECKING, Generic, Optional, TypeVar
5+
6+
from typing_extensions import Self
37

8+
from office365.runtime.client_runtime_context import ClientRuntimeContext
49
from office365.runtime.client_value import ClientValue
510
from office365.runtime.odata.json_format import ODataJsonFormat
611
from office365.runtime.odata.query_options import QueryOptions
712
from office365.runtime.odata.type import ODataType
813
from office365.runtime.odata.v3.json_light_format import JsonLightFormat
14+
from office365.runtime.paths.resource_path import ResourcePath
15+
16+
if TYPE_CHECKING:
17+
from office365.runtime.client_object_collection import ClientObjectCollection
918

10-
T = TypeVar("T", bound="ClientObject")
19+
20+
T = TypeVar("T")
1121
P_T = TypeVar("P_T")
22+
"""Property Type."""
1223

1324

14-
class ClientObject(object):
25+
class ClientObject(Generic[T]):
1526
def __init__(self, context, resource_path=None, parent_collection=None):
16-
"""
17-
Base client object which define named properties and relationships of an entity
18-
19-
:type parent_collection: office365.runtime.client_object_collection.ClientObjectCollection or None
20-
:type resource_path: office365.runtime.paths.resource_path.ResourcePath or None
21-
:type context: office365.runtime.client_runtime_context.ClientRuntimeContext
22-
"""
27+
# type: (ClientRuntimeContext, Optional[ResourcePath], Optional[ClientObjectCollection]) -> None
28+
"""Base client object which define named properties and relationships of an entity."""
2329
self._properties = {}
2430
self._ser_property_names = []
2531
self._query_options = QueryOptions()
@@ -29,9 +35,8 @@ def __init__(self, context, resource_path=None, parent_collection=None):
2935
self._resource_path = resource_path
3036

3137
def clear(self):
32-
"""
33-
Resets client object's state
34-
"""
38+
# type: () -> Self
39+
"""Resets client object's state."""
3540
self._properties = {
3641
k: v
3742
for k, v in self._properties.items()
@@ -42,11 +47,8 @@ def clear(self):
4247
return self
4348

4449
def execute_query(self):
45-
"""
46-
Submit request(s) to the server
47-
48-
:type self: T
49-
"""
50+
# type: () -> Self
51+
"""Submit request(s) to the server."""
5052
self.context.execute_query()
5153
return self
5254

@@ -79,14 +81,13 @@ def after_execute(self, action, *args, **kwargs):
7981
return self
8082

8183
def get(self):
82-
"""
83-
Retrieves a client object from the server
84-
:type self: T
85-
"""
84+
# type: () -> Self
85+
"""Retrieves a client object from the server."""
8686
self.context.load(self)
8787
return self
8888

8989
def is_property_available(self, name):
90+
# type: (str) -> bool
9091
"""Returns a Boolean value that indicates whether the specified property has been retrieved or set.
9192
9293
:param str name: A property name
@@ -96,16 +97,13 @@ def is_property_available(self, name):
9697
return False
9798

9899
def expand(self, names):
99-
"""
100-
Specifies the related resources to be included in line with retrieved resources
101-
102-
:type self: T
103-
:type names: list[str]
104-
"""
100+
# type: (list[str]) -> Self
101+
"""Specifies the related resources to be included in line with retrieved resources."""
105102
self.query_options.expand = names
106103
return self
107104

108105
def select(self, names):
106+
# type: (list[str]) -> Self
109107
"""
110108
Allows to request a limited set of properties
111109
@@ -122,6 +120,7 @@ def remove_from_parent_collection(self):
122120
return self
123121

124122
def _persist_changes(self, name):
123+
# type: (str) -> Self
125124
"""
126125
Marks a property as a serializable
127126
:param str name: A property name
@@ -131,19 +130,15 @@ def _persist_changes(self, name):
131130
return self
132131

133132
def get_property(self, name, default_value=None):
134-
"""
135-
Gets property value
136-
137-
:type name: str
138-
:type default_value: P_T
139-
:rtype: P_T
140-
"""
133+
# type: (str, P_T) -> P_T
134+
"""Gets property value."""
141135
if default_value is None:
142136
normalized_name = name[0].lower() + name[1:]
143137
default_value = getattr(self, normalized_name, None)
144138
return self._properties.get(name, default_value)
145139

146140
def set_property(self, name, value, persist_changes=True):
141+
# type: (str, P_T, bool) -> Self
147142
"""Sets property value
148143
149144
:param str name: Property name

office365/runtime/client_object_collection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Generic, Iterator, Optional, TypeVar
1+
from typing import Generic, Iterator, List, Optional, TypeVar
22

33
from typing_extensions import Self
44

office365/runtime/client_runtime_context.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
from time import sleep
3-
from typing import TypeVar
3+
from typing import TYPE_CHECKING
44

55
from office365.runtime.client_request_exception import ClientRequestException
66
from office365.runtime.client_result import ClientResult
@@ -9,7 +9,8 @@
99
from office365.runtime.queries.client_query import ClientQuery
1010
from office365.runtime.queries.read_entity import ReadEntityQuery
1111

12-
T = TypeVar("T", bound="ClientObject")
12+
if TYPE_CHECKING:
13+
from office365.runtime.client_object import T
1314

1415

1516
class ClientRuntimeContext(object):

office365/sharepoint/excel/excel_rest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ def pending_request(self):
2828
pass
2929

3030
def get_workbook(self, list_name, file_name):
31-
raise NotImplemented("get_workbook")
31+
raise NotImplementedError("get_workbook")

setup.cfg

+23-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ description-file = README.md
55
profile=black
66

77
[flake8]
8+
# These error codes can be fixed, but since it touches many files the
9+
# suggestion is to do it one code at a time to reduce review effort
810
# F401 - imported but unused
9-
extend-ignore = E203, E501, W503, F401
11+
# F841 - local variable defined but never used
12+
extend-ignore = E203, E501, W503, F401, F841
1013
max-line-length = 88
14+
exclude =
15+
generator/metadata
16+
office365/runtime/compat.py
17+
18+
[pylint]
19+
max-line-length=120
20+
# all codes: http://pylint-messages.wikidot.com/all-codes
21+
disable=
22+
C0103,
23+
C0303,
24+
C0111,
25+
C0415, # import-outside-toplevel
26+
C0112, # empty-docstring
27+
C0209, # consider-using f-string
28+
C0114, # missing-module-docstring
29+
R1725, # super-with-arguments
30+
31+
[pylint.FORMAT]
32+
max-line-length = 121

0 commit comments

Comments
 (0)