Skip to content

Provide reusable type definitions #111

Description

@ntessore

To make the construction of bespoke cosmology classes easier, we could provide a set of standard type definitions.

For example, going by our current definitions:

  • ScalarOrArray, which accepts a single scalar or an array
  • Array, which is always an array (since I believe we agreed on returning 0D arrays?) — this exists but isn't part of the standard.

Then it could be idiomatic for library maintainers to define generic cosmology interfaces like this:

ConstantT = TypeVar("ConstantT", bound=cosmology.api.ScalarOrArray)
RedshiftT = TypeVar("RedshiftT", bound=cosmology.api.ScalarOrArray)
ResultT = TypeVar("ResultT", bound=cosmology.api.Array)

class Cosmology(
    cosmology.api.HasC[ConstantT],
    cosmology.api.HasX0[ResultT],
    cosmology.api.HasY[ResultT, RedshiftT],
    Protocol,
): ...

def f(z: RedshiftT, cosmo: Cosmology[ResultT, RedshiftT]) -> ...:
    ...

In the future, we could then introduce refined types for constants, redshifts, wavenumbers, etc. if we ever found them to behave slightly differently.

Besides, we should document that the type args of our protocols are always ordered in the same way: HasX[ResultT, ArgT1, ArgT2], with result first, then first type for arguments, second, etc.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions