Skip to content

Commit

Permalink
type hints enhancements (PEP 484), fixes for methods with the type of…
Browse files Browse the repository at this point in the history
… the enclosing class
  • Loading branch information
vgrem committed Oct 12, 2023
1 parent e33710e commit 475bf9b
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 70 deletions.
11 changes: 5 additions & 6 deletions examples/onedrive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from office365.graph_client import GraphClient
from office365.onedrive.driveitems.driveItem import DriveItem
from office365.onedrive.workbooks.workbook import Workbook
from office365.runtime.client_request_exception import ClientRequestException


def upload_excel_sample(graph_client):
"""
:type graph_client: office365.graph_client.GraphClient
"""
# type: (GraphClient) -> DriveItem
local_path = "../../data/Financial Sample.xlsx"
return graph_client.me.drive.root.resumable_upload(local_path).execute_query()


def ensure_workbook_sample(graph_client):
"""
:type graph_client: office365.graph_client.GraphClient
"""
# type: (GraphClient) -> Workbook
try:
return (
graph_client.me.drive.root.get_by_path("Financial Sample.xlsx")
Expand Down
3 changes: 1 addition & 2 deletions examples/onedrive/files/list_shared_with_me.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
Retrieves a collection of DriveItem resources that have been shared with the current user
"""
from office365.graph_client import GraphClient
from office365.onedrive.driveitems.driveItem import DriveItem
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
drive_items = client.me.drive.shared_with_me().execute_query()
for item in drive_items: # type: DriveItem
for item in drive_items:
print("Drive Item url: {0}".format(item.web_url))
5 changes: 2 additions & 3 deletions examples/onedrive/files/list_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
https://learn.microsoft.com/en-us/graph/api/driveitem-list-versions?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from office365.onedrive.versions.drive_item import DriveItemVersion
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
Expand All @@ -14,5 +13,5 @@
.get()
.execute_query()
)
for ver in file_item.versions: # type: DriveItemVersion
print(ver)
for ver in file_item.versions:
print(ver.id)
4 changes: 2 additions & 2 deletions examples/onedrive/files/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
files, folders, lists, list items, or sites.
https://learn.microsoft.com/en-us/graph/search-concept-files
"""
import json

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
result = client.search.query_drive_items("Guide.docx").execute_query()
print(json.dumps(result.value.to_json(), indent=4))
for item in result.value:
print("Search terms: {0}".format(item.searchTerms))
2 changes: 1 addition & 1 deletion examples/onedrive/files/upload_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def print_progress(range_pos):
.get()
.execute_query()
)
print(f"File {remote_file.web_url} has been uploaded")
print("File {0} has been uploaded".format(remote_file.web_url))
3 changes: 1 addition & 2 deletions examples/onedrive/lists/enum_in_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
"""

from office365.graph_client import GraphClient
from office365.onedrive.lists.list import List
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
lists = client.sites.root.lists.get().execute_query()
for lst in lists: # type: List
for lst in lists:
print(lst.display_name)
4 changes: 2 additions & 2 deletions examples/onedrive/sites/list_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
List site permissions
https://learn.microsoft.com/en-us/graph/api/site-list-permissions?view=graph-rest-1.0&tabs=http
"""
import json

from office365.graph_client import GraphClient
from tests import test_team_site_url
Expand All @@ -12,4 +11,5 @@
permissions = (
client.sites.get_by_url(test_team_site_url).permissions.get().execute_query()
)
print(json.dumps(permissions.to_json(), indent=4))
for perm in permissions:
print(perm)
3 changes: 1 addition & 2 deletions examples/onedrive/sites/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
"""

from office365.graph_client import GraphClient
from office365.onedrive.sites.site import Site
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
sites = client.sites.search("team").execute_query()
for site in sites: # type: Site
for site in sites:
print("Site url: {0}".format(site.web_url))
4 changes: 1 addition & 3 deletions examples/onedrive/workbook/work_with_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"""
from examples.onedrive import ensure_workbook_sample
from office365.graph_client import GraphClient
from office365.onedrive.workbooks.tables.rows.row import WorkbookTableRow
from office365.runtime.client_request_exception import ClientRequestException
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
Expand All @@ -19,7 +17,7 @@
table = workbook.worksheets["Sheet1"].tables["financials"]
# read table content
rows = table.rows.get().execute_query()
for r in rows: # type: WorkbookTableRow
for r in rows:
print(r.values)

print("Refreshing a session...")
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/contenttypes/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from office365.runtime.queries.service_operation import ServiceOperationQuery


class ContentTypeCollection(EntityCollection):
class ContentTypeCollection(EntityCollection[ContentType]):
def __init__(self, context, resource_path):
super(ContentTypeCollection, self).__init__(context, ContentType, resource_path)

Expand Down
33 changes: 16 additions & 17 deletions office365/onedrive/driveitems/driveItem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
from datetime import datetime
from typing import IO, Callable, Optional, TypeVar
from typing import IO, AnyStr, Callable, Optional, TypeVar

from typing_extensions import Self

Expand Down Expand Up @@ -45,6 +47,7 @@
from office365.runtime.client_result import ClientResult
from office365.runtime.client_value_collection import ClientValueCollection
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.odata.v4.upload_session import UploadSession
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.create_entity import CreateEntityQuery
from office365.runtime.queries.function import FunctionQuery
Expand All @@ -60,7 +63,7 @@ class DriveItem(BaseItem):
OneDrive and SharePoint are returned as driveItem resources"""

def get_by_path(self, url_path):
# type: (str) -> DriveItem
# type: (str) -> "DriveItem"
"""
Retrieve DriveItem by server relative path
Expand All @@ -71,7 +74,7 @@ def get_by_path(self, url_path):
)

def create_powerpoint(self, name):
# type: (str) -> DriveItem
# type: (str) -> "DriveItem"
"""
Creates a PowerPoint file
Expand Down Expand Up @@ -179,7 +182,7 @@ def checkin(self, comment, checkin_as=None):
return self

def resumable_upload(self, source_path, chunk_size=2000000, chunk_uploaded=None):
# type: (str, int, Optional[Callable[[int], None]]) -> DriveItem
# type: (str, int, Optional[Callable[[int], None]]) -> "DriveItem"
"""
Create an upload session to allow your app to upload files up to the maximum file size.
An upload session allows your app to upload ranges of the file in sequential API requests,
Expand All @@ -202,11 +205,9 @@ def resumable_upload(self, source_path, chunk_size=2000000, chunk_uploaded=None)
return return_type

def create_upload_session(self, item):
# type: (DriveItemUploadableProperties) -> ClientResult
# type: (DriveItemUploadableProperties) -> ClientResult[UploadSession]
"""Creates a temporary storage location where the bytes of the file will be saved until the complete file is
uploaded.
:type item: office365.graph.onedrive.driveItemUploadableProperties.DriveItemUploadableProperties
"""
qry = UploadSessionQuery(self, {"item": item})
self.context.add_query(qry)
Expand Down Expand Up @@ -239,7 +240,7 @@ def _modify_query(request):
return return_type

def upload_file(self, path_or_file):
# type: (str or IO) -> DriveItem
# type: (str or IO) -> "DriveItem"
"""Uploads a file
:param str or typing.IO path_or_file:
Expand All @@ -255,7 +256,7 @@ def upload_file(self, path_or_file):
return self.upload(name, content)

def get_content(self, format_name=None):
# type: (Optional[str]) -> ClientResult[bytes]
# type: (Optional[str]) -> ClientResult[AnyStr]
"""
Download the contents of the primary stream (file) of a DriveItem.
Only driveItems with the file property can be downloaded.
Expand All @@ -280,9 +281,7 @@ def download(self, file_object):
"""

def _save_content(return_type):
"""
:type return_type: ClientResult
"""
# type: (ClientResult[AnyStr]) -> None
file_object.write(return_type.value)

self.get_content().after_execute(_save_content)
Expand Down Expand Up @@ -325,7 +324,7 @@ def _process_response(response):
return self

def create_folder(self, name, conflict_behavior=ConflictBehavior.Rename):
# type: (str, Optional[ConflictBehavior]) -> DriveItem
# type: (str, Optional[ConflictBehavior]) -> "DriveItem"
"""Create a new folder or DriveItem in a Drive with a specified parent item or path.
:param str name: Folder name
Expand Down Expand Up @@ -363,7 +362,7 @@ def copy(self, name=None, parent=None, conflict_behavior=ConflictBehavior.Fail):
Returns location for details about how to monitor the progress of the copy, upon accepting the request.
"""
return_type = ClientResult(self.context, str())
return_type = ClientResult(self.context) # type: ClientResult[str]

def _create_request(request):
"""
Expand Down Expand Up @@ -440,20 +439,20 @@ def _drive_item_loaded():
return return_type

def rename(self, new_name):
# type: (str) -> DriveItem
# type: (str) -> "DriveItem"
"""Rename a DriveItem
:param str new_name: The new name for the rename.
"""
return self.move(name=new_name)

def search(self, query_text):
# type: (str) -> EntityCollection[DriveItem]
# type: (str) -> EntityCollection["DriveItem"]
"""Search the hierarchy of items for items matching a query. You can search within a folder hierarchy,
a whole drive, or files shared with the current user.
:type query_text: str
"""
return_type = EntityCollection(
return_type = EntityCollection[DriveItem](
self.context, DriveItem, ResourcePath("items", self.resource_path)
)
qry = FunctionQuery(self, "search", {"q": query_text}, return_type)
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/lists/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from office365.runtime.queries.create_entity import CreateEntityQuery


class ListCollection(EntityCollection):
class ListCollection(EntityCollection[List]):
"""Drive list's collection"""

def __init__(self, context, resource_path=None):
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/lists/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class List(BaseItem):
@property
def display_name(self):
"""
The displayable title of the list.
The displayable title of the list
:rtype: str or None
"""
return self.properties.get("displayName", None)
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/permissions/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from office365.runtime.queries.create_entity import CreateEntityQuery


class PermissionCollection(EntityCollection):
class PermissionCollection(EntityCollection[Permission]):
"""Permission's collection"""

def __init__(self, context, resource_path=None):
Expand Down
4 changes: 2 additions & 2 deletions office365/onedrive/sites/sites_with_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from office365.runtime.queries.service_operation import ServiceOperationQuery


class SitesWithRoot(EntityCollection):
class SitesWithRoot(EntityCollection[Site]):
"""Sites container"""

def __init__(self, context, resource_path=None):
Expand Down Expand Up @@ -46,7 +46,7 @@ def remove(self, sites):
return return_type

def search(self, query_text):
# type: (str) -> SitesWithRoot
# type: (str) -> "SitesWithRoot"
"""
Search across a SharePoint tenant for sites that match keywords provided.
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/workbooks/tables/rows/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from office365.onedrive.workbooks.tables.rows.row import WorkbookTableRow


class WorkbookTableRowCollection(EntityCollection):
class WorkbookTableRowCollection(EntityCollection[WorkbookTableRow]):
def __init__(self, context, resource_path=None):
super(WorkbookTableRowCollection, self).__init__(
context, WorkbookTableRow, resource_path
Expand Down
10 changes: 4 additions & 6 deletions office365/runtime/client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from office365.runtime.client_request_exception import ClientRequestException
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.queries.client_query import ClientQuery
from office365.runtime.types.event_handler import EventHandler


Expand All @@ -18,12 +20,8 @@ def __init__(self):

@abstractmethod
def build_request(self, query):
"""
Builds a request
:type query: office365.runtime.queries.client_query.ClientQuery
:rtype: office365.runtime.http.request_options.RequestOptions
"""
# type: (ClientQuery) -> RequestOptions
"""Builds a request"""
pass

@abstractmethod
Expand Down

0 comments on commit 475bf9b

Please sign in to comment.