saiunit.typing.PhysicalType

saiunit.typing.PhysicalType#

class saiunit.typing.PhysicalType(physical_type: str)[source]#

Create a physical type that works with both type annotations and isinstance.

PhysicalType("length") returns a class (type) that can be used:

  1. As isinstance second argument:

    isinstance(5.0 * u.meter, PhysicalType("length"))  # True
    isinstance(5.0 * u.second, PhysicalType("length"))  # False
    
  2. Inside Annotated metadata (via Quantity["length"]):

    def f(x: Quantity["length"]) -> Quantity["time"]: ...
    
Parameters:

physical_type (str) – A human-readable physical type name such as "length", "speed", "voltage", etc.

Returns:

A class whose metaclass implements __instancecheck__ to verify that a Quantity has the correct dimension.

Return type:

type

Examples

>>> from saiunit.typing import PhysicalType
>>> import saiunit as u
>>> pt = PhysicalType("speed")
>>> pt.physical_type
'speed'
>>> isinstance(3.0 * u.meter / u.second, pt)
True
>>> isinstance(3.0 * u.meter, pt)
False
__init__()#

Methods