Skip to content

Ask for an explicit annotation if the inferred type is None #3741

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

Closed
ilevkivskyi opened this issue Jul 19, 2017 · 9 comments
Closed

Ask for an explicit annotation if the inferred type is None #3741

ilevkivskyi opened this issue Jul 19, 2017 · 9 comments

Comments

@ilevkivskyi
Copy link
Member

Currently mypy behaves like this

class C:
    def __init__(self) -> None:
        self.x = None

    def bar(self) -> None:
        self.x = 42  # Incompatible types in assignment (expression has type "int", variable has type None)

This may be non-intuitive for new users. Instead, it is proposed to either:

  • Request an explicit annotation if the inferred type is None, like we would do for lst = [].
  • Use partial None types, like we would do if both assignments were within the same function.

cc @vlasovskikh

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 21, 2017

Requiring an explicit annotation would be consistent with how we treat self.x = []. Partial types don't escape the scope that defines them and I don't want to change this. Marking this as high priority since I believe that this is a regression (the behavior was changed relatively recently, at least when not using strict optional checking).

@gvanrossum
Copy link
Member

I bisected, and it's indeed a regression caused by PR #3024 (Make strict Optional type system changes standard). Ideally we'd have this fixed in the 0.521 release.

@gvanrossum
Copy link
Member

CC @ddfisher

@ddfisher
Copy link
Collaborator

This wasn't a regression exactly, but rather a purposeful decision about how None types should work in strict Optional that we made in the early days. This change in how NoneType was treated was incorporated into the main part of mypy along with the other main typechecking changes from strict Optional.

Also, intending for a variable to have type None based only on initial assignment isn't incredibly uncommon. It happens 21 times in the test suite and 88 times across the Dropbox internal codebase (but it doesn't happen at all in mypy).

Have we actually had any new users confused by this?

@ddfisher
Copy link
Collaborator

If we decide we want to implement this and want it out by release, feel free to merge #3755. Note that this is likely to break at least some user code.

@emmatyping
Copy link
Member

emmatyping commented Jul 23, 2017

Have we actually had any new users confused by this?

@vlasovskikh was confused by the error on the code @ilevkivskyi posted in the original comment. See https://gitter.im/python/typing?at=596faa652723db8d5e28fe6e and following comments.

@vlasovskikh
Copy link
Member

@ethanhs @ddfisher I've added my comment about it to #3755.

@Daenyth
Copy link

Daenyth commented Jul 25, 2017

Have we actually had any new users confused by this?

We've been incorporating mypy into our production workflow and we've had people hit this and be confused. I was able to walk them through it, but I also have dived fairly deep into the docs and have been playing with mypy longer than most people here.

@emmatyping
Copy link
Member

#3755 is closed as wontfix so closing this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants