Skip to content

Default non-optional function parameter is accepted with a default as None. #9077

Closed
@Jiehong

Description

@Jiehong

Hi,

Mypy seems to not be bothered by None as a default function argument, even if that argument is not an Optional.

Here is an example:

def type_check_me(index: int = None) -> str:
    return f"{index}"

Running mypy on this does not lead to any issue.

mypy version: 0.782

Expected:

I would expect mypy to tell me that None is not a valid default value for index, because it has type int.

Instead, the following code should be considered valid:

from typing import Optional

def type_check_me(index: Optional[int] = None) -> str:
    return f"{index}"

Activity

Jiehong

Jiehong commented on Jul 1, 2020

@Jiehong
Author

Note: I tried with --strict-optional (even though it's the default now), with no difference.

kaste

kaste commented on Jul 1, 2020

@kaste
JelleZijlstra

JelleZijlstra commented on Jul 1, 2020

@JelleZijlstra
Member

Maybe we should flip the default on --no-implicit-optional?

JukkaL

JukkaL commented on Jul 3, 2020

@JukkaL
Collaborator

Maybe we should flip the default on --no-implicit-optional?

Yeah, I've been thinking about this. We might want to do it separately from other major breaking changes, such as the switch to a modular typeshed.

JukkaL

JukkaL commented on Jul 3, 2020

@JukkaL
Collaborator

Closing in favor of #9091. Let's continue the discussion there.

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

        Participants

        @kaste@JelleZijlstra@Jiehong@JukkaL

        Issue actions

          Default non-optional function parameter is accepted with a default as None. · Issue #9077 · python/mypy