gdsfactory / kfactory

gdsfactory with a klayout backend
https://gdsfactory.github.io/kfactory/
MIT License
28 stars 10 forks source link

Use `Annotated` to support propagating units to metadata #281

Closed sebastian-goeldi closed 2 months ago

sebastian-goeldi commented 3 months ago

Use something like this in the KCLayout.cell decorator:

from typing import Annotated
import inspect

def f(
    width: Annotated[int, "dbu"],
    length: Annotated[float, "nm"],
    radius: Annotated[float, "um"],
    n: int,
) -> None:
    pass

for parameter in inspect.signature(f).parameters.values():
    parameter.annotation
    if isinstance(parameter.annotation, type):
        print(f"I'm a unitless parameter of type: {parameter.annotation}")
    else:
        print(
            f"I'm a paramerter in {parameter.annotation.__metadata__[0]} and of type: {parameter.annotation.__args__[0]}"
        )

Output:

I'm a paramerter in dbu and of type: <class 'int'>
I'm a paramerter in nm and of type: <class 'float'>
I'm a paramerter in um and of type: <class 'float'>
I'm a unitless parameter of type: <class 'int'>
sebastian-goeldi commented 3 months ago

@joamatab probably interesting for you as well

joamatab commented 3 months ago

looks great, i plan to use all units in um :)

sebastian-goeldi commented 3 months ago

So, how do you measure a count in um, or an an orientation?