Skip to content

Consider a refinement for class variable declarations #209

Closed
@gvanrossum

Description

@gvanrossum

In mypy we are proposing to implement strict Optional checking per PEP 484, with one exception.

python/mypy#1450

The one exception will be in the case of instance variables defined in the class body. These variables may be assigned None while having a non-Optional type, provided that they are initialized to a non-None value by the end of __init__. They may be initialized in __init__ itself, or in any method on self that __init__ calls, or in __new__. Mypy will not check these variables for use before assignment to a non-None value in __init__ or called methods, though it may make sense to add that at some point in the future.

The purpose of this exception is to allow variables to be typed in a common place rather than scattered around __init__.

I think this is a good idea and we should update PEP 484 to follow this.

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