Highlight differences in complex types

Open
#4,531 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start with the issue's Callable example and the current incompatible-assignment diagnostic it shows. Define which typical nested-type differences should be highlighted and how multiple differences or line wrapping should behave; done means the resulting diagnostics make the differing parts clear without relying on the full, nearly identical type strings.

Written by the indexing model from the issue text.

Description

diagnostics topic-error-reporting topic-usability

Sometimes it's hard to see why two types are incompatible if they are long and almost the same. Dummy example:

from typing import Callable
from mypy_extensions import Arg

x: Callable[[Arg(int, 'foobar'), Arg(int, 'bir'), Arg(int, 'fubar')], None]
y: Callable[
    [Arg(int, 'foobar'), Arg(int, 'blr'), Arg(int, 'fubar')], None]

x = y

This is the error:

t.py:7: error: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'foobar'), Arg(int, 'blr'), Arg(int, 'fubar')], None]", variable has type "Callable[[Arg(int, 'foobar'), Arg(int, 'bir'), Arg(int, 'fubar')], None]")

Here's a potential better message that highlights the difference:

t.py:7: error: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'foobar'), Arg(int, 'blr'), Arg(int, 'fubar')], None]", variable has type "Callable[[Arg(int, 'foobar'), Arg(int, 'bir'), Arg(int, 'fubar')], None]")
t.py:7: note: Expression type: ..., Arg(int, 'blr'), ...
t.py:7: note: Variable type:   ..., Arg(int, 'bir'), ...
t.py:7: note:                                 ^^^

It's not clear what's the best way to generalize this sort of "type diff" to arbitrary nested types and to arbitrary type differences. We should at least handle some typical cases. We could also look at how other (non-Python) language implementations handle this.

Just splitting long lines and aligning them would help a bit (but in general the alignment could be off, since there could be multiple differences in types, only some of which are problematic):

t.py:7: error: Incompatible types in assignment (expression has type "Callable[[Arg(int, 'foobar'), Arg(int, 'blr'), Arg(int, 'fubar')], None]", 
t.py:7: error:                                     variable has type "Callable[[Arg(int, 'foobar'), Arg(int, 'bir'), Arg(int, 'fubar')], None]")
Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

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.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.