i2mint / meshed

Link functions up into callable objects
https://i2mint.github.io/meshed/
MIT License
4 stars 3 forks source link

`Sig.names_of_kind` #26

Closed thorwhalen closed 2 years ago

thorwhalen commented 2 years ago

This would be a functools.cached_property of Sig that would give us a tuple containing tuples of names for each kind.

It's behavior would be as such:

from i2 import Sig

def f(a00, /, a11, a12, *a23, a34, a35, a36, **a47): ...

assert Sig(f).names_of_kind == (('a00',), ('a11', 'a12'), ('a23',), ('a34', 'a35', 'a36'), ('a47',))

We would then be able to address issue 25 by putting this validation in Sig.__init__:

        self.names_of_kind = _names_of_kind(self)

        if vps := len(self.names_of_kind[Parameter.VAR_POSITIONAL]) > 1:
            raise ValueError(f"You can't have several variadic keywords: {vps}")
        if vks := len(self.names_of_kind[Parameter.VAR_KEYWORD]) > 1:
            raise ValueError(f"You can't have several variadic keywords: {vks}")

A (valid) critique is that it's an extra overhead in the signature construction, but we get more validation, which is good in the long run for all the meta-programming purposes that we use Sig for.

We keep self.names_of_kind around because with it we can then compute several other properties and method more easily/quickly with it around.

thorwhalen commented 2 years ago

Deprecated (warning) Sig.names_for_kind(kind) method and replaced all known usages (in code, note notebooks) with the new names_of_kind[kind] property.

thorwhalen commented 2 years ago

I will make going through the properties and methods of Sig and replacing relevant code with use of names_of_kind a separate issue and assign to @sylvainbonnot or @valentin-feron.

thorwhalen commented 2 years ago

Moved to https://github.com/i2mint/i2/issues/23 where it belongs