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

stubgen: Support TypedDict alternative syntax #14682

Merged
merged 13 commits into from May 6, 2023

Conversation

hamdanal
Copy link
Contributor

Fixes #14681

mypy/stubgen.py Outdated Show resolved Hide resolved
mypy/stubgen.py Outdated Show resolved Hide resolved
mypy/stubgen.py Outdated Show resolved Hide resolved
mypy/stubgen.py Outdated Show resolved Hide resolved
self.add(f"{self._indent}{lvalue.name} = {rvalue.accept(p)}\n")
self._state = VAR
else:
bases = "TypedDict"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy actually now supports generic TypedDicts defined with the call syntax, in which case TypedDict wouldn't be the only base (the rewritten version using the class-based syntax would use multiple inheritance with Generic[T] in this playground example): https://mypy-play.net/?mypy=latest&python=3.11&gist=fbeb5bbd0c3036b7327fc65fff0c9a9d

I think it's reasonable not to handle generic TypedDicts defined using the call syntax in this PR, since they're unlikely to come up much. But probably worth a TODO comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, thank you.
I'll add a TODO for now. Generic TypedDict requires keeping track of TypeVars defined in the file which deserves its own PR IMO.

mypy/stubgen.py Outdated
self._state = VAR
else:
bases = "TypedDict"
if len(rvalue.args) > 2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if a user does something like

Foo = TypedDict("Foo", {"a": int}, b=str, d=bytes)

Defining TypedDicts like this using keyword arguments is deprecated at runtime, and has never been supported by mypy — but according to the typing docs it is (or was until recently, when we deprecated it) a supported way of creating a new TypedDict type: https://docs.python.org/3/library/typing.html#typing.TypedDict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right again Alex. Indeed I forgot about the keyword syntax.
The example you gave however is invalid at runtime:

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import TypedDict
>>> Foo = TypedDict("Foo", {"a": int}, b=str, d=bytes)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/typing.py", line 2435, in TypedDict
    raise TypeError("TypedDict takes either a dict or keyword arguments,"
TypeError: TypedDict takes either a dict or keyword arguments, but not both

this works:

>>> from typing import TypedDict
>>> Foo = TypedDict("Foo", a=int, b=str, d=bytes)
>>> Foo.__required_keys__
frozenset({'b', 'd', 'a'})
>>> Foo.__optional_keys__
frozenset()

I'll update the PR to add support for the keyword syntax.

return
items.append((attr_name.value, attr_type))
if len(rvalue.args) > 2:
total = rvalue.args[2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it would be good to be more cautious here. What if somebody runs stubgen on code like this?

T = TypedDict("T", {"a": int}, b=str, total=False)

Now, you'll tell me that this code raises TypeError at runtime, and indeed it does! But that doesn't mean that stubgen should crash (or do something incorrect) if it's run on code like this. We should just emit T: Incomplete and move on to the next thing in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more checks for invalid cases including this one. I also added a test for importing from typing_extensions.

With all the checks for invalid uses, the TypedDict handling part looks much more conservative than other parts of stubgen. I am not sure what to make of this, it is not necessarily a positive or negative thing, just something I noticed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With all the checks for invalid uses, the TypedDict handling part looks much more conservative than other parts of stubgen. I am not sure what to make of this, it is not necessarily a positive or negative thing, just something I noticed.

Hmm, well, I'm not really an expert on stubgen, so I can't really comment on decisions that have previously been taken. But in my opinion, it's always good for static analysis tools to be as paranoid as possible about the kind of thing that might be fed to them :)

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting to look pretty good, from what I can tell! Could you add a test case where TypedDict is imported from typing_extensions in the source code stubgen's being run on?

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an expert on stubgen, but this LGTM as far as I can tell!

@JelleZijlstra JelleZijlstra merged commit d710fdd into python:master May 6, 2023
14 checks passed
@hamdanal hamdanal deleted the stubgen-typeddict branch May 6, 2023 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stubgen: Support Alternative syntax of TypedDict
3 participants