mathause / filefinder

find and parse file and folder names
MIT License
3 stars 1 forks source link

allow `"{keys}"` as placeholder (use positional-only argument for `keys`) #100

Open mathause opened 3 hours ago

mathause commented 3 hours ago

we could use a positional-only argument for keys

def test(keys, /, **kwargs):
    print(keys, kwargs)

test({"a": 5}, keys=7) # works
# {'a': 5} {'keys': 7}

def test(keys, **kwargs):
    print(keys, kwargs)

test({"a": 5}, keys=7) # fails
# TypeError: test() got multiple values for argument 'keys'

Originally posted by @mathause in https://github.com/mathause/filefinder/issues/97#issuecomment-2402366535

This would allow us to use "{keys}" as placeholder. However, optional positional-only keywords in conjunction with **kwargs are bothersome: you need to do all the validity checks yourself.

mathause commented 3 hours ago

I think that would be the correct validity check but we need to keep it even after the deprecation period. So maybe not worth it.

def _check_keys_args_kwargs(keys_args, keys_kwargs, keys):

    if "keys" not in keys:
        if "keys" in keys_kwargs:
            if keys_args is not None:
                raise TypeError()
            else:
                warnings.warn("deprecated")
                # raise TypeError() # after the deprecation period
                return keys_kwargs.pop("keys"), keys_kwargs

    if "keys" in keys_kwargs and isinstance(keys_kwargs["keys"], dict):
        raise TypeError()

    return keys_args, keys_kwargs