Skip to content

Distinct formatting for type objects #3374

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

Merged
merged 2 commits into from
May 18, 2017

Conversation

elazarg
Copy link
Contributor

@elazarg elazarg commented May 16, 2017

Fix #3050. (The discussion there is not over yet, but this PR can demonstrate the formatting)

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks! This mostly looks good to me, just one suggestion and one comment unrelated to this PR.

@@ -1505,7 +1505,7 @@ T = TypeVar('T')
class C(Generic[T]):
def __init__(self) -> None: pass
x = C # type: Callable[[], C[int]]
y = C # type: Callable[[], int] # E: Incompatible types in assignment (expression has type C[T], variable has type Callable[[], int])
y = C # type: Callable[[], int] # E: Incompatible types in assignment (expression has type Type[C[T]], variable has type Callable[[], int])
Copy link
Member

Choose a reason for hiding this comment

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

Probably unrelated to this PR, but this error message looks not right, it should be Type[C] here (or maybe Type[C[Any]]). If not and it is not easy to fix this (by just erasing the return type) in the same PR, could you please open a new issue?

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 am also not entirely sure I understand what's wrong with Type[C[T]]. Making this work seems to require either bringing erasure to this module or duplicating the formatting of instance types without type parameters.

Copy link
Member

Choose a reason for hiding this comment

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

I am also not entirely sure I understand what's wrong with Type[C[T]]

PEP 484 specifies that bare (unsubscribed) type is equivalent to C[Any]. Moreover, writing C[T] is invalid in this context (and in other examples I mentioned below) and will trigger a mypy error Invalid type '__main__.T'.

Does fixing this actually requires extensive code duplication? If yes, then maybe it is better to import erasetype.

@@ -1096,7 +1096,7 @@ class MyDDict(t.DefaultDict[int,T], t.Generic[T]):
MyDDict(dict)['0']
MyDDict(dict)[0]
[out]
_program.py:6: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str]
_program.py:6: error: Argument 1 to "defaultdict" has incompatible type Type[List[_T]]; expected Callable[[], str]
Copy link
Member

Choose a reason for hiding this comment

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

Again, this should be Type[List] or Type[List[Any]].

@@ -1479,7 +1479,7 @@ L = Callable[[Arg(name='x', type=int)], int] # ok
# I have commented out the following test because I don't know how to expect the "defined here" note part of the error.
# M = Callable[[Arg(gnome='x', type=int)], int] E: Invalid type alias E: Unexpected keyword argument "gnome" for "Arg"
N = Callable[[Arg(name=None, type=int)], int] # ok
O = Callable[[List[Arg(int)]], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: Type expected within [...] # E: The type List[T] is not generic and not indexable
O = Callable[[List[Arg(int)]], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: Type expected within [...] # E: The type Type[List[T]] is not generic and not indexable
Copy link
Member

Choose a reason for hiding this comment

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

E: The type Type[List[T]] is not generic and not indexable.

@sixolet This error looks quite confusing in this context, since the type mentioned is generic and is indexable. In real mypy installation (not in tests) I get these errors:

tst26.py:14: error: Invalid type alias
tst26.py:14: error: Value of type "object" is not indexable
tst26.py:14: error: Type expected within [...]
tst26.py:14: error: Value of type List[_T] is not indexable

Note that none of them mentions (or even hints) that something is wrong with Arg. Is there an issue for improving the error message in such cases?

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

LGTM. If no one objects, then I will merge this soon.

@ilevkivskyi ilevkivskyi merged commit 3cd62e1 into python:master May 18, 2017
@gvanrossum
Copy link
Member

Thank you! (Both.)

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

Successfully merging this pull request may close these issues.

3 participants