Skip to content

Support for no_type_check decorator #645

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 5 commits into from
May 12, 2015
Merged

Conversation

spkersten
Copy link
Contributor

Implements #557.

I've renamed the no_type_check decorator to no_type_checks.

Currently the typexport-basic tests fails because the no_type_check decorator I added to the typing.py stub shows up in the output of all tests.

@gvanrossum
Copy link
Member

Why the rename? AFAIK the e PEP had the he singular.
On Apr 19, 2015 11:14 AM, "Sander Kersten" [email protected] wrote:

Implements #557 #557.

I've renamed the no_type_check decorator to no_type_checks.

Currently the typexport-basic tests fail for a weird reason.

You can view, comment on, or merge this pull request online at:

#645
Commit Summary

  • Renamed no_type_check decorator to no_type_checks.
  • Parse annotations of function decorated with 'no_type_checks' as
    regular expression instead of type annotations.
  • Don't type check functions decorated with 'no_type_checks'

File Changes

Patch Links:


Reply to this email directly or view it on GitHub
#645.

@spkersten
Copy link
Contributor Author

@gvanrossum Both [https://github.com/python/typing/issues/26](your comment) and #557 have the plural form. But I'll change it back to what the PEP say.

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 29, 2015

Thanks for the PR! I've been busy recently, but I had time for a quick a pass.

The implementation has a small limitation in that it detects the decorator based on the name. Something like this would not be detected:

from typing import no_type_check as my_annotations

@my_annotations
def foo(x: {1: 'x'}) -> 3: ...

I think this would also not work, which is may be a more realistic use case:

import typing

@typing.no_type_check
def foo(...) -> 2: ... 

To implement these properly, we could accept arbitrary expressions in annotations during parsing, and only translate them to types during semantic analysis (but only if there is no no_type_check_ decorator). Not sure if this is important enough to implement now, but it would be more consistent with how names are bound elsewhere.

@gvanrossum
Copy link
Member

I think the 'as' rename is uninteresting, but the @typing.no_type_check
form should be supported.

On Tuesday, April 28, 2015, Jukka Lehtosalo [email protected]
wrote:

Thanks for the PR! I've been busy recently, but I had time for a quick a
pass.

The implementation has a small limitation in that it detects the decorator
based on the name. Something like this would not be detected:

from typing import no_type_check as my_annotations
@my_annotationsdef foo(x: {1: 'x'}) -> 3: ...

I think this would also not work, which is may be a more realistic use
case:

import typing
@typing.no_type_checkdef foo(...) -> 2: ...

To implement these properly, we could accept arbitrary expressions in
annotations during parsing, and only translate them to types during
semantic analysis (but only if there is no no_type_check_ decorator). Not
sure if this is important enough to implement now, but it would be more
consistent with how names are bound elsewhere.


Reply to this email directly or view it on GitHub
#645 (comment).

--Guido van Rossum (on iPad)

@spkersten
Copy link
Contributor Author

This is what I got stuck on; I wasn't happy with the string comparison on 'no_type_check'. Parsing types only after semantic analysis seemed the best solution, but is a lot more work.

If it's okay with you, I'll keep the string comparison for now (with both 'no_type_check' and 'typing.no_type_check'), as this feature is quite useful given the existing bugs/limitations in mypy. Separate of this, I'll try to figure out how to move the parsing of type to the semantic analysis phases.

On 29 apr. 2015, at 07:06, Jukka Lehtosalo [email protected] wrote:

Thanks for the PR! I've been busy recently, but I had time for a quick a pass.

The implementation has a small limitation in that it detects the decorator based on the name. Something like this would not be detected:

from typing import no_type_check as my_annotations

@my_annotations
def foo(x: {1: 'x'}) -> 3: ...
I think this would also not work, which is may be a more realistic use case:

import typing

@typing.no_type_check
def foo(...) -> 2: ...
To implement these properly, we could accept arbitrary expressions in annotations during parsing, and only translate them to types during semantic analysis (but only if there is no no_type_check_ decorator). Not sure if this is important enough to implement now, but it would be more consistent with how names are bound elsewhere.


Reply to this email directly or view it on GitHub.

@spkersten spkersten changed the title Support for no_type_checks decorator Support for no_type_check decorator May 2, 2015
@spkersten
Copy link
Contributor Author

@JukkaL The no_type_check decorator I added to the typing.py stub shows up in the output of many typexport-basic tests. The comments intesttypegen.py suggest that it is not easy to suppress that from the output; and the things I tried were unsuccessful. Do you have any ideas how to fix this?

@JukkaL
Copy link
Collaborator

JukkaL commented May 3, 2015

Here's a diff that fixes the test failures:

diff --git a/mypy/test/data/lib-stub/typing.py b/mypy/test/data/lib-stub/typing.py
index c96fa78..35d9977 100644
--- a/mypy/test/data/lib-stub/typing.py
+++ b/mypy/test/data/lib-stub/typing.py
@@ -16,6 +16,7 @@ Callable = object()
 builtinclass = object()
 _promote = object()
 NamedTuple = object()
+no_type_check = object()

 # Type aliases.
 List = object()
@@ -35,6 +36,3 @@ class Iterator(Iterable[T], Generic[T]):
 class Sequence(Generic[T]):
     @abstractmethod
     def __getitem__(self, n: Any) -> T: pass
-
-def no_type_check(func):
-    return func
\ No newline at end of file

The current convention is to initialize all 'special' names in typing.py stubs with object(). The initializer is actually arbitrary and it might be slightly better to use something like ... (literal ellipsis).

@spkersten
Copy link
Contributor Author

Thanks for the explanation of how the stub works. I've applied your diff and fixed the handling of the @typing.no_type_check case.

@JukkaL
Copy link
Collaborator

JukkaL commented May 12, 2015

Looks good now! Thanks again for the PR!

JukkaL added a commit that referenced this pull request May 12, 2015
Support for no_type_check decorator
@JukkaL JukkaL merged commit edd2b64 into python:master May 12, 2015
@spkersten spkersten deleted the no_type_checks branch May 12, 2015 16:32
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