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

Type checking for **kwds based on its use #12991

Closed
mauvilsa opened this issue Jun 17, 2022 · 1 comment
Closed

Type checking for **kwds based on its use #12991

mauvilsa opened this issue Jun 17, 2022 · 1 comment
Labels

Comments

@mauvilsa
Copy link

Feature

Enable type checking when **kwds does not have a type annotation but what it accepts can be deduced based on its use.

Pitch

Look at the following example:

def some_function(f1: int):
    assert isinstance(f1, int)

def other_function(f0: str, **kwds):
    assert isinstance(f0, str)
    some_function(**kwds)

other_function(f0='a', f1='b')

I would expect a static type checker to warn that f1='b' is wrong because the type should be int. There shouldn't be a need to add a type to **kwds since what is accepted can be deduced from how it is used. Note that some_function could be defined in another module and be used in multiple places, thus the "don't repeat yourself" and "separation of concerns" principles apply. Better to have the type for f1 be only in the definition of some_function and not in the **kwds.

This example is similar to #6552, but there it says that it is a duplicate of #4441 which is about typing of **kwds. Why is the solution for this adding a type to **kwds instead of the type checker analyzing how **kwds is used? There are cases in which it is not possible to derive the types based on the use of **kwds. For those certainly makes sense to add a type to it.

Some context as to why I am asking this. I have been developing jsonargparse which can be used for automatically creating complex parsers based on signatures. Figuring out the accepted keyword args and its types based on how **kwds is used, to me seems like a natural feature to expect. jsonargparse 4.10.0.dev2 already supports many cases like this, see AST resolver. I just wonder why static type checkers are not going in this direction.

@AlexWaygood
Copy link
Member

As @erictraut explains well in the pyright issue you also opened, this change would require a change in the spec. As this is something that would affect all type checkers and users of Python typing, it shouldn't be discussed on the mypy issue tracker. If you'd like to discuss this, please open an issue at https://github.com/python/typing or on the typing-sig mailing list

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants