MCPcopy Create free account
hub / github.com/ipython/traitlets / __init__

Method __init__

traitlets/traitlets.py:1345–1404  ·  view source on GitHub ↗
(self, *args: t.Any, **kwargs: t.Any)

Source from the content-addressed store, hash-verified

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:
1384 self.notify_change(changes[key])
1385
1386 try:
1387 super().__init__(*super_args, **super_kwargs)
1388 except TypeError as e:
1389 arg_s_list = [repr(arg) for arg in super_args]
1390 for k, v in super_kwargs.items():
1391 arg_s_list.append(f"{k}={v!r}")
1392 arg_s = ", ".join(arg_s_list)
1393 warn(
1394 "Passing unrecognized arguments to super({classname}).__init__({arg_s}).\n"
1395 "{error}\n"
1396 "This is deprecated in traitlets 4.2."
1397 "This error will be raised in a future release of traitlets.".format(
1398 arg_s=arg_s,
1399 classname=self.__class__.__name__,
1400 error=e,
1401 ),
1402 DeprecationWarning,

Callers

nothing calls this directly

Calls 9

has_traitMethod · 0.95
set_traitMethod · 0.95
notify_changeMethod · 0.95
BunchClass · 0.85
warnFunction · 0.85
_cross_validateMethod · 0.80
appendMethod · 0.80
formatMethod · 0.80
__init__Method · 0.45

Tested by

no test coverage detected