Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Jiehong opened this issue Jul 1, 2020 · 5 comments
Closed

Comments

@Jiehong
Copy link

Jiehong commented Jul 1, 2020

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}"
@Jiehong
Copy link
Author

Jiehong commented Jul 1, 2020

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

@kaste
Copy link

kaste commented Jul 1, 2020

I think you're looking for --no-implicit-optional https://mypy.readthedocs.io/en/stable/config_file.html#none-and-optional-handling

@JelleZijlstra
Copy link
Member

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

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 3, 2020

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
Copy link
Collaborator

JukkaL commented Jul 3, 2020

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

@JukkaL JukkaL closed this as completed Jul 3, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants