| 1324 | |
| 1325 | |
| 1326 | class HasTraits(HasDescriptors, metaclass=MetaHasTraits): |
| 1327 | _trait_values: dict[str, t.Any] |
| 1328 | _static_immutable_initial_values: dict[str, t.Any] |
| 1329 | _trait_notifiers: dict[str | Sentinel, t.Any] |
| 1330 | _trait_validators: dict[str | Sentinel, t.Any] |
| 1331 | _cross_validation_lock: bool |
| 1332 | _traits: dict[str, t.Any] |
| 1333 | _all_trait_default_generators: dict[str, t.Any] |
| 1334 | |
| 1335 | def setup_instance(self, /, *args: t.Any, **kwargs: t.Any) -> None: |
| 1336 | # although we'd prefer to set only the initial values not present |
| 1337 | # in kwargs, we will overwrite them in `__init__`, and simply making |
| 1338 | # a copy of a dict is faster than checking for each key. |
| 1339 | self._trait_values = self._static_immutable_initial_values.copy() |
| 1340 | self._trait_notifiers = {} |
| 1341 | self._trait_validators = {} |
| 1342 | self._cross_validation_lock = False |
| 1343 | super(HasTraits, self).setup_instance(*args, **kwargs) |
| 1344 | |
| 1345 | def __init__(self, *args: t.Any, **kwargs: t.Any) -> None: |
| 1346 | # Allow trait values to be set using keyword arguments. |
| 1347 | # We need to use setattr for this to trigger validation and |
| 1348 | # notifications. |
| 1349 | super_args = args |
| 1350 | super_kwargs = {} |
| 1351 | |
| 1352 | if kwargs: |
| 1353 | # this is a simplified (and faster) version of |
| 1354 | # the hold_trait_notifications(self) context manager |
| 1355 | def ignore(change: Bunch) -> None: |
| 1356 | pass |
| 1357 | |
| 1358 | self.notify_change = ignore # type:ignore[method-assign] |
| 1359 | self._cross_validation_lock = True |
| 1360 | changes = {} |
| 1361 | for key, value in kwargs.items(): |
| 1362 | if self.has_trait(key): |
| 1363 | setattr(self, key, value) |
| 1364 | changes[key] = Bunch( |
| 1365 | name=key, |
| 1366 | old=None, |
| 1367 | new=value, |
| 1368 | owner=self, |
| 1369 | type="change", |
| 1370 | ) |
| 1371 | else: |
| 1372 | # passthrough args that don't set traits to super |
| 1373 | super_kwargs[key] = value |
| 1374 | # notify and cross validate all trait changes that were set in kwargs |
| 1375 | changed = set(kwargs) & set(self._traits) |
| 1376 | for key in changed: |
| 1377 | value = self._traits[key]._cross_validate(self, getattr(self, key)) |
| 1378 | self.set_trait(key, value) |
| 1379 | changes[key]["new"] = value |
| 1380 | self._cross_validation_lock = False |
| 1381 | # Restore method retrieval from class |
| 1382 | del self.notify_change |
| 1383 | for key in changed: |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…