Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Optional types not being narrowed after a logical-and followed by a test of one of the operands #6835

Open
arusahni opened this issue May 15, 2019 · 2 comments
Labels
false-positive mypy gave an error on correct code feature priority-2-low

Comments

@arusahni
Copy link

(Sorry if the title is overly specific)

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Optional

def get_org_id(org_name: str) -> str:
    return 'some_org_id'

def create(org_name: Optional[str], org_id: Optional[str]):
    if not org_name and not org_id:
        raise ValueError('No org specified')
    if not org_id:
        org_id = get_org_id(org_name)
  • What is the actual behavior/output?

The inferred type of org_name at the last line is Optional[str] (technically Union[builtins.str, None]).

  • What is the behavior/output you expect?

The type should be narrowed to str.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Python 3.7.3
mypy: 0.701

  • What are the mypy flags you are using? (For example --strict-optional)

No flags

@ilevkivskyi
Copy link
Member

This is to tricky for mypy to understand. We may support this at some point, but I thin kit is relatively low priority. Se also #6641 which is quite similar.

@ilevkivskyi ilevkivskyi added feature priority-2-low false-positive mypy gave an error on correct code labels May 15, 2019
@ghost
Copy link

ghost commented Apr 28, 2022

@ilevkivskyi #10696 might also be a duplicate

maybe I'm mistaken, but i believe this might be a bug?
this only occurs in some cases; i don't understand the issue enough to describe which cases

one example i've seen is some definition of f like f = () -> Optional[str] which terminates with return A and A.b where A is some class, and b is some str attribute of A will result in an error indicating
Incompatible return value type (got "Union[A, str]", expected "Optional[str]")

you know already that the correct type is Union[str, None]

also, this sometimes? works correctly with things like it: str = (A and A.b) or 'nice'

edit: some weird libs make their own falsey types; i.e. Union[Unset, T]...but don't support them, imo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature priority-2-low
Projects
None yet
Development

No branches or pull requests

2 participants