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

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

Open
CmdQ opened this issue Apr 21, 2021 · 2 comments
Open
Labels
bug mypy got something wrong

Comments

@CmdQ
Copy link

CmdQ commented Apr 21, 2021

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 List and dict literals with explicit types not documented #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
@CmdQ CmdQ added the bug mypy got something wrong label Apr 21, 2021
@erictraut
Copy link

The pastebin link appears to no longer work. For future reference, here's an inlined minimal repro of the problem:

from datetime import datetime

def func(split_date: list[int], split_time: list[int]):
    datetime(*split_date, *split_time)
    datetime(*split_date, *split_time, tzinfo=None)

@eltoder
Copy link

eltoder commented Aug 26, 2023

Similar example with a mypy-play link: https://mypy-play.net/?mypy=latest&python=3.11&gist=b8e03e17ed8edd5caf12f17db0e488a7

from datetime import datetime, UTC

def utc_time(*args) -> datetime:
    return datetime(*args, tzinfo=UTC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants