55import importlib
66import warnings
77from collections.abc import Sequence
8- from typing import Any, Generic, TypeVar, Union
8+ from typing import Any, Generic, TypeVar
99
1010import numpy as np
1111import prettytable
@@ -38,7 +38,7 @@ class ClassList(collections.UserList, Generic[T]):
3838
3939 """
4040
41- def __init__(self, init_list: Union[ Sequence[T], T] = None, name_field: str = "name") -> None:
41+ def __init__(self, init_list: Sequence[T] | T = None, name_field: str = "name") -> None:
4242 self.name_field = name_field
4343
4444 # Set input as list if necessary
@@ -114,7 +114,7 @@ def __str__(self):
114114 output = str(self.data)
115115 return output
116116
117- def __getitem__(self, index: Union[ int, slice, str, T] ) -> T:
117+ def __getitem__(self, index: int | slice | str | T ) -> T:
118118 """Get an item by its index, name, a slice, or the object itself."""
119119 if isinstance(index, (int, slice)):
120120 return self.data[index]
@@ -262,12 +262,12 @@ def insert(self, index: int, obj: T = None, **kwargs) -> None:
262262 self._validate_name_field(kwargs)
263263 self.data.insert(index, self._class_handle(**kwargs))
264264
265- def remove(self, item: Union[T, str] ) -> None:
265+ def remove(self, item: T | str) -> None:
266266 """Remove an object from the ClassList using either the object itself or its ``name_field`` value."""
267267 item = self._get_item_from_name_field(item)
268268 self.data.remove(item)
269269
270- def count(self, item: Union[T, str] ) -> int:
270+ def count(self, item: T | str) -> int:
271271 """Return the number of times an object appears in the ClassList.
272272
273273 This method can use either the object itself or its ``name_field`` value.
@@ -276,7 +276,7 @@ def count(self, item: Union[T, str]) -> int:
276276 item = self._get_item_from_name_field(item)
277277 return self.data.count(item)
278278
279- def index(self, item: Union[T, str] , offset: bool = False, *args) -> int:
279+ def index(self, item: T | str, offset: bool = False, *args) -> int:
280280 """Return the index of a particular object in the ClassList.
281281
282282 This method can use either the object itself or its ``name_field`` value.
@@ -309,7 +309,7 @@ def union(self, other: Sequence[T]) -> None:
309309 ]
310310 )
311311
312- def set_fields(self, index: Union[ int, slice, str, T] , **kwargs) -> None:
312+ def set_fields(self, index: int | slice | str | T , **kwargs) -> None:
313313 """Assign the values of an existing object's attributes using keyword arguments."""
314314 self._validate_name_field(kwargs)
315315 pydantic_object = False
@@ -519,7 +519,7 @@ def _check_classes(self, input_list: Sequence[T]) -> None:
519519 f"In the input list:\n{newline.join(error for error in error_list)}\n"
520520 )
521521
522- def _get_item_from_name_field(self, value: Union[T, str] ) -> Union[T, str] :
522+ def _get_item_from_name_field(self, value: T | str) -> T | str:
523523 """Return the object with the given value of the ``name_field`` attribute in the ClassList.
524524
525525 Parameters
@@ -577,11 +577,12 @@ def _determine_class_handle(input_list: Sequence[T]):
577577 @classmethod
578578 def __get_pydantic_core_schema__(cls, source: Any, handler):
579579 # import here so that the ClassList can be instantiated and used without Pydantic installed
580+ from typing import get_args, get_origin
581+
580582 from pydantic import ValidatorFunctionWrapHandler
581583 from pydantic.types import (
582584 core_schema, # import core_schema through here rather than making pydantic_core a dependency
583585 )
584- from typing_extensions import get_args, get_origin
585586
586587 # if annotated with a class, get the item type of that class
587588 origin = get_origin(source)
0 commit comments