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

Don't interpret arguments with a default of None as Optional #3248

Merged
merged 3 commits into from May 26, 2017

Conversation

ddfisher
Copy link
Collaborator

Fix python/typing#275.

(Likely needs a bit more discussion.)

@gvanrossum
Copy link
Member

It would be much better if this started out as a command-line flag. (Probably global, unless it's really easy to make it per-module.)

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 26, 2017

I agree that this would be better as a flag, at least in the next release or two.

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

Please make this a flag. (Global if you have to, per-mod if it's easy.)

@ddfisher
Copy link
Collaborator Author

ddfisher commented May 4, 2017

Updated to use an off-by-default per-module flag.

@gvanrossum
Copy link
Member

I haven't looked in detail at your code yet, but when I tried this, the typeshed tests for Python 2.7 started failing without enabling the new flag. To repro, check out this PR, then

cd typeshed
./tests/mypy_test.py -p2

I don't want to post the entire list of errors (too long and boring) but here are the first few and last few:

running mypy --python-version 2.7 --strict-optional # with 566 files
stdlib/2/__builtin__.pyi:29: error: Incompatible types in assignment (expression has type "Optional[str]", base class "object" defined the type as "Optional[str]")
stdlib/2/__builtin__.pyi:30: error: Incompatible types in assignment (expression has type "__builtin__.type", base class "object" defined the type as "builtins.type")
stdlib/2/__builtin__.pyi:31: error: Incompatible types in assignment (expression has type "Union[str, unicode, Iterable[Union[str, unicode]], None]", base class "object" defined the type as "Union[str, unicode, Iterable[Union[str, unicode]], None]")
stdlib/2/__builtin__.pyi:32: error: Incompatible types in assignment (expression has type "__builtin__.str", base class "object" defined the type as "builtins.str")
stdlib/2/__builtin__.pyi:36: error: Argument 1 of "__setattr__" incompatible with supertype "object"
stdlib/2/__builtin__.pyi:37: error: Argument 1 of "__eq__" incompatible with supertype "object"
stdlib/2/__builtin__.pyi:37: error: Return type of "__eq__" incompatible with supertype "object"
stdlib/2/__builtin__.pyi:38: error: Argument 1 of "__ne__" incompatible with supertype "object"
stdlib/2/__builtin__.pyi:38: error: Return type of "__ne__" incompatible with supertype "object"
stdlib/2/__builtin__.pyi:39: error: Return type of "__str__" incompatible with supertype "object"
.
.
.
stdlib/2/__builtin__.pyi:970: error: Argument 1 of "seek" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:970: error: Argument 2 of "seek" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:970: error: Return type of "seek" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:971: error: Return type of "tell" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:972: error: Argument 1 of "readline" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:972: error: Return type of "readline" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:973: error: Argument 1 of "readlines" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:973: error: Return type of "readlines" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:974: error: Argument 1 of "write" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:974: error: Return type of "write" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:975: error: Argument 1 of "writelines" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:976: error: Argument 1 of "truncate" incompatible with supertype "IO"
stdlib/2/__builtin__.pyi:976: error: Return type of "truncate" incompatible with supertype "IO"
--- exit status 1 ---

# as unicode instead of bytes. This hack is generally okay,
# because mypy considers str literals to be compatible with
# unicode.
return StrExpr(n.s)
Copy link
Member

Choose a reason for hiding this comment

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

These changes seem at first unrelated to --no-implicit-optional -- why are they here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made this change in lieu of updating self.pyversion -> self.options.python_version because we've asserted that this is a Python 3 or stub file in the constructor (so only one codepath is ever taken here).

Copy link
Member

Choose a reason for hiding this comment

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

Ah, OK. There should be no major version checks in fastparse{,2}.py at all.

Copy link
Member

Choose a reason for hiding this comment

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

(In the light of the bug you fixed, as a follow-up, it seems there should be no major version checks in fastparse2.py, but those in fastparse.py should be kept, since much of that file is still shared with fastparse2.py.)

@gvanrossum
Copy link
Member

So I can still repro the issue with typeshed. Can you look into that?

@ddfisher
Copy link
Collaborator Author

Yep! I repro'd them too. I was overzealous in removing version checks -- one is actually needed in fastparse.py (see the commit I just pushed which fixes it).

@gvanrossum gvanrossum merged commit 443c479 into master May 26, 2017
@gvanrossum gvanrossum deleted the no-implicit-optional branch May 26, 2017 14:47
@gvanrossum
Copy link
Member

I tried to use this and realized it only has an effect when --strict-optional is also set. Fine.

But the error that it produces is "Incompatible types in assignment (expression has type None, variable has type ...)".

I think it deserves a more specialized error, e.g. "Default for argument XXX is incompatible with its type (expression has type None, argument has type ...)".

If you agree, can you file a new issue to track that task?

@JukkaL
Copy link
Collaborator

JukkaL commented May 26, 2017

Might be worth explicitly adding a note that suggests using Optional[...] after the error, and maybe also mention that implicit optional types for arguments are no longer supported.

@gvanrossum
Copy link
Member

... are no longer supported

Should probably be "are not supported with --no-implicit-optional".

carljm added a commit to carljm/mypy that referenced this pull request May 30, 2017
* master: (23 commits)
  Make return type of open() more precise (python#3477)
  Add test cases that delete a file during incremental checking (python#3461)
  Parse each format-string component separately (python#3390)
  Don't warn about returning Any if it is a proper subtype of the return type (python#3473)
  Add __setattr__ support (python#3451)
  Remove bundled lib-typing (python#3337)
  Move version of extensions to post-release (python#3348)
  Fix None slice bounds with strict-optional (python#3445)
  Allow NewType subclassing NewType. (python#3465)
  Add console scripts (python#3074)
  Fix 'variance' label.
  Change label for variance section to just 'variance' (python#3429)
  Better error message for invalid package names passed to mypy (python#3447)
  Fix last character cut in html-report if file does not end with newline (python#3466)
  Print pytest output as it happens (python#3463)
  Add mypy roadmap (python#3460)
  Add flag to avoid interpreting arguments with a default of None as Optional (python#3248)
  Add type checking plugin support for functions (python#3299)
  Mismatch of inferred type and return type note (python#3428)
  Sync typeshed (python#3449)
  ...
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.

None yet

3 participants