python / python/mypy

New type inference processes `itertools.groupby` key as `Any`

Open
#16,986 0 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

Given code like:

import itertools

for grouper, members in itertools.groupby('aBcD', key=lambda x: x.isupper()):
    reveal_type(grouper)
    reveal_type(members)

Running mypy at 1.6.1 without arguments or on any more recent version with --old-type-inference results in the expected output:

$ mypy demo.py --old-type-inference
demo.py:4: note: Revealed type is "builtins.bool"
demo.py:5: note: Revealed type is "typing.Iterator[builtins.str]"
Success: no issues found in 1 source file

However on more recent versions without --old-type-inference we get:

$ mypy demo.py 
demo.py:4: note: Revealed type is "Any"
demo.py:5: note: Revealed type is "typing.Iterator[builtins.str]"
Success: no issues found in 1 source file

Original case

In my original use-case, the grouper key was a tuple which I was spraying into a function call:

    for source, errors_group in itertools.groupby(errors, key=lambda x: x.source):
            messages = [x.message for x in errors_group]
            report_errors(*source, messages)

mypy was previously happy with that, however since the change to process the grouper key as Any I now get errors:

sr/comp/validation.py:113:13: error: Too many arguments for "report_errors"  [call-arg]

Your Environment

  • Mypy version used: bisected to 93d4cb0a2ef1723ce92f39ae61fe6a0c010eb90b being the commit which introduced the change (from which I then tested --old-type-inference); I realise that commit doesn't introduce the new inference logic, though I haven't chased beyond that commit
  • Mypy command-line flags: none required
  • Mypy configuration options from mypy.ini (and other config files): none required
  • Python version used: 3.10

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 by running the demo.py reproduction with and without --old-type-inference, then inspect the change around commit 93d4cb0a2ef1723ce92f39ae61fe6a0c010eb90b. Done means the itertools.groupby key is revealed as builtins.bool while the members remain typing.Iterator[builtins.str], without breaking the tuple-key call shown in the original case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.