-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Process NameTuple and fix Union types using | #13788
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -516,6 +516,17 @@ __all__ = ['urllib'] | |
[out] | ||
import urllib as urllib | ||
|
||
|
||
[case testImportUnion] | ||
def func(a: str | int) -> str | int: | ||
pass | ||
|
||
[out] | ||
from typing import Union | ||
|
||
def func(a: Union[str, int]) -> Union[str, int]: ... | ||
|
||
|
||
[case testRelativeImportAll] | ||
from .x import * | ||
[out] | ||
|
@@ -595,6 +606,33 @@ class X(NamedTuple): | |
a: Incomplete | ||
b: Incomplete | ||
|
||
[case testNamedtupleUsingInvalidIdent] | ||
import collections, x | ||
X = collections.namedtuple('X', ['@']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that it is correct. It just drops a field that is present in the original code. I think a proper way to fix is to use There are several typeshed examples where we actually do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generating X = collections.namedtuple('X', ['@', 'a', 'b', 'c']) in the stubs would create invalid stub code. |
||
[out] | ||
from typing import NamedTuple | ||
|
||
class X(NamedTuple): ... | ||
|
||
[case testNamedtupleWithTypesInvalidIdent] | ||
import collections, x | ||
X = typing.NamedTuple('X', [('@', int)]) | ||
[out] | ||
from typing import NamedTuple | ||
|
||
class X(NamedTuple): ... | ||
|
||
[case testNamedtupleWithTypes] | ||
import collections, x | ||
X = typing.NamedTuple('X', [('a', str), ('b', str)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please also test a case like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sobolevn, won't that fail at runtime? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will fail at runtime: I have added isidentifier validation. |
||
[out] | ||
from _typeshed import Incomplete | ||
from typing import NamedTuple | ||
|
||
class X(NamedTuple): | ||
a: str | ||
b: str | ||
|
||
[case testEmptyNamedtuple] | ||
import collections | ||
X = collections.namedtuple('X', []) | ||
|
@@ -915,7 +953,7 @@ T = TypeVar('T') | |
alias = Union[T, List[T]] | ||
|
||
[out] | ||
from typing import TypeVar | ||
from typing import TypeVar, Union | ||
|
||
T = TypeVar('T') | ||
alias = Union[T, List[T]] | ||
|
@@ -2705,3 +2743,19 @@ def f(): | |
return 0 | ||
[out] | ||
def f(): ... | ||
|
||
[case testClassNamedtuple_semanal] | ||
import typing | ||
import collections, x | ||
class X(typing.NamedTuple("X", [("a", int), ("b", int)])): | ||
def m(self): | ||
pass | ||
|
||
[out] | ||
from _typeshed import Incomplete | ||
from typing import NamedTuple | ||
|
||
class X(NamedTuple): | ||
a: int | ||
b: int | ||
def m(self) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this match something like
notanamedtuple
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I don't know what mypy support for "namedtuple" is, but I wanted the same behaviour for Namedtuple (NameExpr).
Original code was:
I just added NamedTuple :)
collections.namedtuple vs typing.NamedTuple.
It seems that NameExpr can be qualified name:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both
NameExpr
andMemberExpr
have.fullname
attribute. Maybe it is better to use it instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm I tried ... but fullname is resolved only when running stubgen from cmd line but not when running unit tests (it is None) and all namedtuple tests failing. Any idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So for fullname I need _semanal tests otherwise parseonly is enabled for unit tests. I can switch to semanal for namedtuple tests but it seems excessive. Thoughts?