Skip to content

Commit 8a84a89

Browse files
committed
remove hard coded imports
1 parent d210000 commit 8a84a89

File tree

2 files changed

+29
-57
lines changed

2 files changed

+29
-57
lines changed

src/msgraph_core/models/page_result.py

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Classes:
1010
PageResult: Represents a page of items in a paged response.
1111
"""
12-
from typing import Any, List, Optional
12+
from typing import List, Optional, Dict, Callable
1313
from dataclasses import dataclass
1414
from __future__ import annotations
1515

@@ -23,70 +23,41 @@
2323

2424

2525
@dataclass
26-
class PageResult(Parsable, Generic[T]):
27-
"""
28-
Represents a page of items in a paged response.
29-
"""
30-
object_type: Optional[Any] = None
26+
class PageResult(Parsable):
3127
odata_next_link: Optional[str] = None
32-
value: Optional[List[T]] = None
28+
value: Optional[List[Parsable]] = None
3329

3430
@staticmethod
35-
def create_from_discriminator_value(parse_node: ParseNode) -> PageResult: # pylint: disable=unused-argument
31+
def create_from_discriminator_value(parse_node: Optional[ParseNode] = None) -> PageResult:
3632
"""
37-
Creates a PageResult from a discriminator value.
38-
Returns:
39-
PageResult: The created PageResult.
33+
Creates a new instance of the appropriate class based on discriminator value
34+
Args:
35+
parseNode: The parse node to use to read the discriminator value and create the object
36+
Returns: Attachment
4037
"""
41-
impprt_statement = f"from msgraph.generated.models.{str(PageResult.object_type).lower()} \
42-
import {PageResult.object_type}"
43-
44-
# pylint: disable=exec-used
45-
exec(impprt_statement)
46-
if isinstance(PageResult.object_type, str):
47-
return PageResult(locals()[PageResult.object_type])
38+
if not parse_node:
39+
raise TypeError("parse_node cannot be null")
4840
return PageResult()
4941

50-
def set_value(self, value: List[Any]):
51-
"""
52-
Sets the items in the page.
53-
54-
Args:
55-
value (List[Any]): The items to set.
56-
"""
57-
self.value = value
42+
def get_field_deserializers(self) -> Dict[str, Callable[[ParseNode], None]]:
43+
"""Gets the deserialization information for this object.
5844
59-
def get_field_deserializers(self):
60-
"""
61-
Gets the field deserializers for the PageResult.
6245
Returns:
63-
Dict[str, Callable]: The field deserializers.
46+
Dict[str, Callable[[ParseNode], None]]: The deserialization information for this
47+
object where each entry is a property key with its deserialization callback.
6448
"""
65-
class_name = PageResult.object_type
66-
# pylint: disable=not-callable
67-
instance = class_name()
68-
module_path = instance.__class__.__module__
69-
class_name = instance.__class__.__name__
70-
import_statement = f'from {module_path} import {class_name}'
71-
# pylint: disable=exec-used
72-
exec(import_statement)
73-
serialization_model = locals()[class_name]
7449
return {
75-
'@odata.nextLink':
76-
lambda parse_node: setattr(self, 'odata_next_link', parse_node.get_str_value()),
77-
'odata.deltaLink':
78-
lambda parse_node: setattr(self, 'odata_delta_link', parse_node.get_str_value()),
79-
'value':
80-
lambda parse_node: self.
81-
set_value(parse_node.get_collection_of_object_values(serialization_model))
50+
"@odata.nextLink": lambda x: setattr(self, "odata_next_link", x.get_str_value()),
51+
"value": lambda x: setattr(self, "value", x.get_collection_of_object_values(Parsable))
8252
}
8353

8454
def serialize(self, writer: SerializationWriter) -> None:
85-
"""
86-
Serializes the PageResult into a SerializationWriter.
55+
"""Writes the objects properties to the current writer.
56+
8757
Args:
88-
writer (SerializationWriter): The writer to serialize into.
58+
writer (SerializationWriter): The writer to write to.
8959
"""
90-
writer.write_str_value('@odata.nextLink', self.odata_next_link, self.value)
91-
if self.value is not None:
92-
writer.write_collection_of_object_values(None, list(self.value))
60+
if not writer:
61+
raise TypeError("Writer cannot be null")
62+
writer.write_str_value("@odata.nextLink", self.odata_next_link)
63+
writer.write_collection_of_object_values("value", self.value)

src/msgraph_core/tasks/page_iterator.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ def __init__(
6464
self.request_adapter = request_adapter
6565

6666
if isinstance(response, Parsable) and not constructor_callable:
67-
constructor_callable = getattr(type(response), 'create_from_discriminator_value')
67+
parsable_factory = type(response)
6868
elif constructor_callable is None:
69-
constructor_callable = PageResult.create_from_discriminator_value
69+
parsable_factory = PageResult
70+
self.parsable_factory = parsable_factory
7071
self.pause_index = 0
7172
self.headers: HeadersCollection = HeadersCollection()
7273
self.request_options = [] # type: ignore
@@ -201,10 +202,10 @@ async def fetch_next_page(self) -> dict:
201202
request_info.headers = self.headers
202203
if self.request_options:
203204
request_info.add_request_options(*self.request_options)
204-
parsable_factory: PageResult[Any] = PageResult(self.object_type)
205205
error_map: Dict[str, int] = {}
206-
response = await self.request_adapter.send_async(request_info, parsable_factory, error_map)
207-
206+
response = await self.request_adapter.send_async(
207+
request_info, self.parsable_factory, error_map
208+
)
208209
return response
209210

210211
def enumerate(self, callback: Optional[Callable] = None) -> bool:

0 commit comments

Comments
 (0)