Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit 8874486

Browse files
committed
Removed mypy overloads in the Entity class. Use UTC from dateutil everywhere.
1 parent 9049981 commit 8874486

11 files changed

Lines changed: 212 additions & 171 deletions

File tree

cookbook/entities/tutorial7_urbanmobility.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#
1010
# Author: Fabien BATTELLO <fabien.battello@orange.com> et al.
1111

12-
from datetime import datetime, timezone
12+
from datetime import datetime
13+
from dateutil.tz import UTC
1314
from ngsildclient import Entity, PostalAddressBuilder, OpeningHoursBuilder
1415
from ngsildclient.utils.urn import Urn
1516

@@ -23,7 +24,7 @@ def build_entity():
2324
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
2425
],
2526
)
26-
e.tprop("dateModified", datetime(2018, 9, 25, 8, 32, 26, tzinfo=timezone.utc))
27+
e.tprop("dateModified", datetime(2018, 9, 25, 8, 32, 26, tzinfo=UTC))
2728
e.prop("source", "https://api.smartsantander.eu/")
2829
e.prop("dataProvider", "http://www.smartsantander.eu/")
2930
e.prop("entityVersion", "2.0")

docs/source/build.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ observedAt
211211
.. code-block::
212212
:caption: SO2 concentration with the observation date
213213
214-
from datetime import datetime, timezone
214+
from datetime import datetime
215+
from dateutil.tz import UTC
215216
216-
entity.prop("SO2", 11, observedat=datetime(2016, 3, 15, 11, tzinfo=timezone.utc))
217+
entity.prop("SO2", 11, observedat=datetime(2016, 3, 15, 11, tzinfo=UTC))
217218
218219
# Alternatively one could pass directly an ISO8601 string
219220
# entity.prop("SO2", 11, observedat="2016-03-15T11:00:00Z")

docs/source/cookbook.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,8 @@ SmartCities_ on SmartDataModels repository.
18831883
.. code-block::
18841884
:caption: UrbanMobility code snippet
18851885
1886-
from datetime import datetime, timezone
1886+
from datetime import datetime
1887+
from dateutil.tz import UTC
18871888
from ngsildclient import Entity, PostalAddressBuilder, OpeningHoursBuilder
18881889
from ngsildclient.utils.urn import Urn
18891890
@@ -1895,7 +1896,7 @@ SmartCities_ on SmartDataModels repository.
18951896
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
18961897
],
18971898
)
1898-
e.tprop("dateModified", datetime(2018, 9, 25, 8, 32, 26, tzinfo=timezone.utc))
1899+
e.tprop("dateModified", datetime(2018, 9, 25, 8, 32, 26, tzinfo=UTC))
18991900
e.prop("source", "https://api.smartsantander.eu/")
19001901
e.prop("dataProvider", "http://www.smartsantander.eu/")
19011902
e.prop("entityVersion", "2.0")

src/ngsildclient/model/entity.py

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from datetime import datetime
2424
from typing import (
2525
Sequence,
26-
overload,
2726
Any,
2827
Union,
2928
List,
@@ -186,15 +185,24 @@ class Entity:
186185
>>> e.rm("NO2.accuracy")
187186
"""
188187

189-
@overload
190-
def __init__(self, type: str, id: str, *, ctx: List = None):
188+
def __init__(
189+
self,
190+
*args: str,
191+
ctx: List = None,
192+
payload: dict = None,
193+
autoprefix: Optional[bool] = None,
194+
):
191195
"""Create a NGSI-LD compliant entity
192196
193-
One can omit the urn and namespace, "urn:ngsi-ld:" will be added automatically.
194-
One can omit the type inside the identifier.
197+
Expected args are : the Entity's type and identifier.
198+
Example : Entity("AirQualityObserved", "RZ:Obsv4567")
199+
The fully qualified identifier is built following the naming convention : "urn:ngsi-ld:{type}:{id}'.
200+
"urn:ngsi-ld:" is added if not specified.
201+
202+
Alernatively a single arg is allowed : the fully qualified Entity's identifier.
203+
Example : Entity("urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567")
204+
Type is inferred from the fully qualified identifier.
195205
196-
By default, the constructor assumes the identifier naming convention "urn:ngsi-ld:<type>:<remainder>" and
197-
automatically insert the type into the identifier.
198206
The default behaviour can be disabled : Entity.globalsettings.autoprefix = False.
199207
200208
@@ -224,60 +232,16 @@ def __init__(self, type: str, id: str, *, ctx: List = None):
224232
"type": "AirQualityObserved"
225233
}
226234
"""
227-
...
228-
229-
@overload
230-
def __init__(self, id: str, *, ctx: List = None):
231-
"""Create a NGSI-LD compliant entity.
232-
233-
Type is inferred from the fully qualified identifier.
234-
Works only if your identifiers follow the naming convention "urn:ngsi-ld:<type>:<remainder>"
235-
236-
Parameters
237-
----------
238-
id : str
239-
entity identifier (fully qualified urn)
240-
context : list, optional
241-
the NGSI-LD context, by default the NGSI-LD Core Context
242-
243-
Example
244-
-------
245-
>>> from ngsildclient.model.entity import Entity
246-
>>> e = Entity("urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567")
247-
>>> e.pprint()
248-
{
249-
"@context": [
250-
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
251-
],
252-
"id": "urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567",
253-
"type": "AirQualityObserved"
254-
}
255-
"""
256-
...
257-
258-
def __init__(
259-
self,
260-
arg1: str = None,
261-
arg2: str = None,
262-
*,
263-
ctx: List = None,
264-
payload: dict = None,
265-
autoprefix: Optional[bool] = None,
266-
):
267-
logger.debug(f"{arg1=} {arg2=}")
268-
269-
if not ctx:
270-
ctx = [CORE_CONTEXT]
271-
272-
if autoprefix is None:
273-
autoprefix = globalsettings.autoprefix
274-
275235
self.root: NgsiDict = None
276236
self._lastprop: NgsiDict = None
277237
self._anchored: bool = False
278238
self._lastwasmulti: bool = False
279239

280-
if payload is not None: # create Entity from a dictionary
240+
if len(args) == 2:
241+
type, id = args
242+
elif len(args) == 1:
243+
id, type = args[0], None
244+
elif len(args) == 0 and payload is not None: # create Entity from a dictionary
281245
if not payload.get("id", None):
282246
raise NgsiMissingIdError()
283247
if not payload.get("type", None):
@@ -286,14 +250,15 @@ def __init__(
286250
raise NgsiMissingContextError()
287251
self._lastprop = self.root = NgsiDict(payload)
288252
return
289-
290-
# create a new Entity using its id and type
291-
292-
if arg2:
293-
type, id = arg1, arg2
294253
else:
295-
type, id = None, arg1
254+
raise ValueError("Expecteds args : type, id (alt. fully qualified id)")
296255

256+
if autoprefix is None:
257+
autoprefix = globalsettings.autoprefix
258+
if not ctx:
259+
ctx = [CORE_CONTEXT]
260+
261+
# create a new Entity using its id and type
297262
if type is None: # try to infer type from the fully qualified identifier
298263
id = Urn.prefix(id)
299264
urn = Urn(id)

tests/common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
if TYPE_CHECKING:
1717
from ngsildclient.model.constants import EntityOrId
1818

19-
from datetime import datetime, timezone
19+
from datetime import datetime
20+
from dateutil.tz import UTC
2021
from ngsildclient.model.entity import Entity
2122
from ngsildclient.api.client import Client
2223
from ngsildclient.utils.urn import Urn
2324

24-
sample_entity = Entity("AirQualityObserved", "AirQualityObserved:RZ:Obsv4567")
25-
sample_entity.tprop("dateObserved", datetime(2018, 8, 7, 12, tzinfo=timezone.utc))
25+
sample_entity = Entity("AirQualityObserved", "RZ:Obsv4567")
26+
sample_entity.tprop("dateObserved", datetime(2018, 8, 7, 12, tzinfo=UTC))
2627
sample_entity.prop("NO2", 22, unitcode="GP")
2728
sample_entity.rel("refPointOfInterest", "PointOfInterest:RZ:MainSquare")
2829

30+
2931
class MockedClient(Client):
3032
def __init__(self):
3133
self._broker_impl: dict[str, Entity] = {}
@@ -35,4 +37,4 @@ def get(self, entity: EntityOrId) -> Entity:
3537
return self._broker_impl[Urn.prefix(eid)]
3638

3739
def upsert(self, entities: List[Entity]):
38-
self._broker_impl |= {e.id: e for e in entities}
40+
self._broker_impl |= {e.id: e for e in entities}

tests/smartdatamodels/smartcities/test_urbanmobility.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
import pkg_resources
1313
import json
1414

15-
from datetime import datetime, timezone
16-
from ngsildclient.model.entity import *
15+
from datetime import datetime
16+
from dateutil.tz import UTC
17+
from ngsildclient.model.entity import Entity
1718
from ngsildclient.model.helper.postal import PostalAddressBuilder
1819
from ngsildclient.model.helper.openinghours import OpeningHoursBuilder
1920
from ngsildclient.utils.urn import Urn
2021

2122

2223
def expected_dict(basename: str) -> dict:
23-
filename: str = pkg_resources.resource_filename(
24-
__name__, f"data/urbanmobility/{basename}.json"
25-
)
24+
filename: str = pkg_resources.resource_filename(__name__, f"data/urbanmobility/{basename}.json")
2625
with open(filename, "r") as fp:
2726
expected = json.load(fp)
2827
return expected
@@ -41,19 +40,13 @@ def test_transportstop():
4140
],
4241
)
4342

44-
e.tprop("dateModified", datetime(2018, 9, 25, 8, 32, 26, tzinfo=timezone.utc))
43+
e.tprop("dateModified", datetime(2018, 9, 25, 8, 32, 26, tzinfo=UTC))
4544
e.prop("source", "https://api.smartsantander.eu/")
4645
e.prop("dataProvider", "http://www.smartsantander.eu/")
4746
e.prop("entityVersion", "2.0")
4847

4948
builder = PostalAddressBuilder()
50-
address = (
51-
builder.street("C/ La Pereda 14")
52-
.locality("Santander")
53-
.region("Cantabria")
54-
.country("Spain")
55-
.build()
56-
)
49+
address = builder.street("C/ La Pereda 14").locality("Santander").region("Cantabria").country("Spain").build()
5750
e.prop("address", address)
5851

5952
e.gprop("location", (43.478053126, -3.804648385))

tests/test_attr.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
import pytest
1313

14-
from datetime import datetime, date, time, timezone
14+
from datetime import datetime
15+
from dateutil.tz import UTC
1516
from ngsildclient.model.entity import Entity
1617
from ngsildclient.model.attr.prop import AttrPropValue
1718

19+
1820
def test_observedat():
1921
e = Entity("OffStreetParking", "Downtown1")
2022
e.prop("availableSpotNumber", 121, observedat=datetime(2017, 7, 29, 12, 5, 2))
2123
p = e["availableSpotNumber"]
2224
assert isinstance(p, AttrPropValue)
2325
dt = p.observedat
24-
assert dt == datetime(2017, 7, 29, 12, 5, 2, tzinfo=timezone.utc)
26+
assert dt == datetime(2017, 7, 29, 12, 5, 2, tzinfo=UTC)

0 commit comments

Comments
 (0)