Skip to content

Assume unannotated return values to be None instead of Any (behind flag) #14180

Closed as not planned
@berzi

Description

@berzi

Feature

Behind a config flag like untyped_return_type_is_none = true, Mypy assumes that a function with no return type, e.g.:

def my_func(i_am_annotated: str):  # Note: no "-> int" annotation
    return 5

Returns None, instead of accepting any return type at face value.

Additionally, while untyped_return_type_is_none is true, disallow_untyped_defs allows functions to have no return annotation if their return type is None.

Pitch

Adoption for the disallow_untyped_defs is difficult, because it requires None to be explicitly annotated. This makes code more wordy in many places where the annotation adds zero value (think of all test functions/methods for extremely popular frameworks like pytest or unittest). This is especially overwhelming for maintainers of large codebases with many tests and existing functions.

Having the flag would give users an option to leave -> None functions unannotated.

Current behaviour would be unchanged if the new flag were not activated, but activating the flag would force users to annotate those functions that do return a value, providing a large but progressive step towards disallow_untyped_defs, reducing the essentially meaningless menial work of adding -> None everywhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featuretopic-inferenceWhen to infer types or require explicit annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions