python / python/mypy

error in set unions

Open
#12,760 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-context
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

In the following file, mypy (version 0.950) reports (correctly) ok function as ok, but raises an error (error: Argument 1 to "union" of "set" has incompatible type "Set[str]"; expected "Set[Optional[str]]") in error function.

from typing import Set
from zoneinfo import available_timezones

x = {"UTC"}


def error() -> Set[str]:
    return available_timezones() - set.union(x)


def ok() -> Set[str]:
    a, b = available_timezones(), set.union(x)
    return a - b

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with mypy 0.950 using the two functions shown in the report, comparing the direct expression with the intermediate variables. Trace the set-union type-checking path and add a regression test covering both forms; done means the valid error function no longer produces the incompatible-argument diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.