Skip to content

Correct implementation of standard types besides "object" #1340

Merged
gvanrossum merged 5 commits intopython:masterfrom
mistermocha:mistermocha/notindexable
Jun 23, 2017
Merged

Correct implementation of standard types besides "object" #1340
gvanrossum merged 5 commits intopython:masterfrom
mistermocha:mistermocha/notindexable

Conversation

@mistermocha
Copy link
Copy Markdown
Contributor

As per python/mypy#2989, these changes help to correctly indicate usage of Tuple[] among others.

Tested this using this script:

from typing import Tuple, Dict, Generic, Callable, Type, ClassVar

Tuple[int]
Tuple[str]
Tuple[int, int]
Dict[str, int]
Tuple[int, Dict[str, str]]
Generic[int]
Callable[int]
Type[int]
ClassVar[int]

Comment thread stdlib/2/typing.pyi Outdated
no_type_check = object()
ClassVar = object()

class _GenericBaseTypeClass:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer calling this _SpecialForm, which is shorter and more accurate.

@JelleZijlstra
Copy link
Copy Markdown
Member

Looks good, and I like making the typing stubs a bit less magical. This is a bit risky though, so I want to wait for comments from people maintaining type checkers before merging it.

@JelleZijlstra
Copy link
Copy Markdown
Member

Looks like flake8 isn't happy. Can you try writing things as Tuple: _SpecialForm = ...?

Copy link
Copy Markdown
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.

I think in addition to this we need some special casing in mypy, to show tailored error messages if something is wrong with special forms in runtime context.

Comment thread stdlib/2/typing.pyi Outdated
ClassVar = object()

class _SpecialForm:
def __getitem__(self, typeargs: Any) -> Any: ...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would rather say -> object: ... otherwise Tuple[int].nonexisting(1, 'whatever') will be valid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, the return type must not be Any to avoid false negatives. The Any argument type also results in false negatives. It allows things like Tuple[1], which will fail at runtime.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Address this please.

Comment thread stdlib/2/typing.pyi
def __getitem__(self, typeargs: Any) -> Any: ...

Tuple: _SpecialForm = ...
Generic: _SpecialForm = ...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would not change this for Generic, it should never appear in runtime context, IOW it is "too special" :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure how it's different from the others.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can't use Generic in an annotation -- it can only occur in a list of base classes. That's pretty different.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can't use Generic in an annotation -- it can only occur in a list of base classes. That's pretty different.

This is exactly what I mean.

Copy link
Copy Markdown
Contributor

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

I'm not sure if this can be implemented as a typeshed change in a meaningful way, unfortunately. What's the intended use case?

Comment thread stdlib/2/typing.pyi Outdated
ClassVar = object()

class _SpecialForm:
def __getitem__(self, typeargs: Any) -> Any: ...
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, the return type must not be Any to avoid false negatives. The Any argument type also results in false negatives. It allows things like Tuple[1], which will fail at runtime.

@JelleZijlstra
Copy link
Copy Markdown
Member

I suggested doing things this way because it can solve the mypy issue without adding additional special-casing within mypy itself. More generally, I'd like to move the typing stubs to a state where they more closely reflect how typing.py is implemented at runtime, so that tools that use typeshed don't need custom implementations of as many parts of typing.

Obviously a change like this isn't going to be perfect in terms of rejecting all things that fail at runtime, but I generally prefer false negatives over false positives.

Comment thread stdlib/2/typing.pyi Outdated
no_type_check = object()
ClassVar = object()

class _SpecialForm:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Inherit from object explicitly in the PY2 version.

Copy link
Copy Markdown
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

See previous comments (not just mine).

Comment thread stdlib/2/typing.pyi Outdated
ClassVar = object()

class _SpecialForm:
def __getitem__(self, typeargs: Any) -> Any: ...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Address this please.

@JelleZijlstra
Copy link
Copy Markdown
Member

I think everything has been addressed here. @gvanrossum can you take another look?

@gvanrossum gvanrossum merged commit 5306d6f into python:master Jun 23, 2017
@gvanrossum
Copy link
Copy Markdown
Member

Does this actually fix python/mypy#2989? If so please close that (and add a note).

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.

5 participants