Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mathause opened this issue Oct 10, 2024 · 1 comment
Open

Comments

@mathause
Copy link
Collaborator

mathause commented Oct 10, 2024

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
Copy link
Collaborator Author

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

@mathause mathause changed the title use positional-only argument for keys allow "{keys}" as placeholder (use positional-only argument for keys) Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant