Skip to content

mypy doesn't understand set operations on optional #13192

Closed as not planned
python/typeshed
#11403
@Palpatineli

Description

@Palpatineli

I have tried to search for similar issues but failed because I can't find the keywords to limit the open issues to a manageable list. Sorry if it's indeed an old bug. Hope someone here can help

Bug Report

mypy doesn't seem to understand that Set[Optional[T]] minus None is just Set[T]

To Reproduce

from typing import Set
a: Set[str | None] = {'a', None, 'b'}
b: Set[str] = a - {None}
c = ','.join(b)
b2: Set[str] | None = None if None in a else a

Expected Behavior

both line 3 and line 5 should be correct

Actual Behavior

> python3 -m mypy temp.py
temp.py:3: error: Incompatible types in assignment (expression has type "Set[Optional[str]]", variable has type "Set[str]")
temp.py:5: error: Incompatible types in assignment (expression has type "Optional[Set[Optional[str]]]", variable has type "Optional[Set[str]]")
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: none
  • Python version used: 3.10.4
  • Operating system and version: Ubuntu 22.04LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions