Skip to content

How to specify constants of type NoneType? #2

Closed

Description

@matthiaskramm

PEP 484 says "By default, None is an invalid value for any type". However, in the Python standard library there are class-level and module-level constants that are legitimately None.

Examples:

  formatter.py.AS_IS
  hmac.digest_size
  cgi.FieldStorage.FieldStorageClass
  copy.PyStringMap

Should we allow these to be declared using e.g. something like

  AS_IS = Undefined(NoneType)

?

Activity

  1. vlasovskikh commented on Jun 26, 2015

    @vlasovskikh
    Member

    The variables could be of their concrete type or of an optional concrete type depending on whether it is desirable to have the check for None enabled for them.

    For example, a PEP 484 compatible definition of hmac.digest_size could look like this:

    digest_size = ...  # type: int

    or like this:

    digest_size = ... # type: Optional[int]
  2. gvanrossum commented on Jan 26, 2016

    @gvanrossum
    Member

    Maybe one of the following should be declared best practice?

    foo = None  # type: object
    foo = None  # type: Optional[object]
    

    All of these work today with mypy.

  3. matthiaskramm commented on Jan 26, 2016

    @matthiaskramm
    ContributorAuthor

    What about

    foo = None  # type: None
    

    It doesn't work in mypy yet, but mypy's error message sounds like it could, in the future.

  4. gvanrossum commented on Jan 27, 2016

    @gvanrossum
    Member
  5. added 2 commits that reference this issue on Jul 5, 2016
    5f110c7
    8dc6c1e
  6. gvanrossum commented on Oct 27, 2016

    @gvanrossum
    Member

    This is almost(ish :-) over. With --strict-optional, mypy does not complain about

    x = None # type: None
    

    We'll just have to finish for the strict None checking flag in mypy to become the default.

  7. added a commit that references this issue on Dec 29, 2016
  8. added a commit that references this issue on Apr 23, 2017
    efb6e8a
  9. JelleZijlstra commented on Apr 23, 2017

    @JelleZijlstra
    Member

    x = None # type: None works fine in mypy with python/mypy#3024 merged.

    I'm submitting a PR to fix the specific objects @matthiaskramm mentioned.

  10. added a commit that references this issue on Apr 24, 2017
    6f5c6b5
  11. added a commit that references this issue on Jul 16, 2018
  12. added a commit that references this issue on Apr 8, 2021
  13. added a commit that references this issue on Dec 6, 2021
  14. added a commit that references this issue on Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions