python / python/mypy

Unpacking int arguments to datetime.datetime gives a complaint about tzinfo

Open
#10,348 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy complains about this line of code. First I don't understand why and second I cannot bring it to accept it, even when including tzinfo=None in the parameter list.

# error: "datetime" gets multiple values for keyword argument "tzinfo"
dt.datetime(*split_int_string(date, 4, 6, 8), *split_int_string(time, 2, 4))

# or

# error: "datetime" gets multiple values for keyword argument "tzinfo"
dt.datetime(*split_int_string(date, 4, 6, 8), *split_int_string(time, 2, 4), tzinfo=None)

Why does mypy think tzinfo would already be used, it cannot be from a List[int]. The behavior when running is as expected.

The helper function is annotated like this:

def split_int_string(digits: str, *at: int) -> List[int]:
    """
    Split a string at given indices and parse the parts to int.
    """
    # Make a canonical representation.
    split: List[int] = sorted(at)
    if not split or split[0] != 0:
        split.insert(0, 0)
    if split[-1] != len(digits):
        split.append(len(digits))
    return [int(digits[split[i - 1] : split[i]]) for i in range(1, len(split))]

To Reproduce

(Write your steps here:)

  1. Paste from here into a file.
  2. Run with python3, it works.
  3. Pass to mypy to see the error.

Expected Behavior

I expect not to get a typing error for that line.

Actual Behavior

It prints

error: Argument 1 to "datetime" has incompatible type "*List[int]"; expected "Optional[tzinfo]"

or

error: "datetime" gets multiple values for keyword argument "tzinfo"

depending on whether one also adds the tzinfo=None or not.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.6
  • Operating system and version: Linux CL003356 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

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

Start with the pasted reproducer linked in the issue and inspect how mypy analyzes datetime.datetime calls with multiple starred List[int] arguments. Confirm the diagnostic involving tzinfo, then compare it with the runtime behavior shown in the report. Done means the valid call no longer produces the incorrect typing error while genuine argument conflicts remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.