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

Use alias names in error messages #2968

Open
rhettinger opened this issue Mar 6, 2017 · 4 comments
Open

Use alias names in error messages #2968

rhettinger opened this issue Mar 6, 2017 · 4 comments
Labels

Comments

@rhettinger
Copy link

Given:

from typing import Tuple
from math import hypot

Point = Tuple[float, ...]
Centroid = Point

def dist(p: Centroid, q: Point) -> float:
    px, py = p
    qx, qy = q
    return hypot(px - qx, py - qy)

print( dist('xyz', (3.5, 7.2)) )

mypy detect the incorrect first argument:

$ mypy tmp.py
tmp.py:12: error: Argument 1 to "dist" has incompatible type "str"; expected Tuple[float, ...]

However, it would be much nicer is the error reporting could have used the alias name:

tmp.py:12: error: Argument 1 to "dist" has incompatible type "str"; expected "Centroid", which expands to Tuple[float, ...]

I realize it may be easy to do, but it would improve the usability of aliases.

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 6, 2017

Agreed that your proposed message would be better. Mypy currently expands aliases eagerly and doesn't keep track of the name of the alias, so this would be somewhat non-trivial to support.

@Fulguritude
Copy link

Since this feature clearly isn't a priority to the maintainers; could I have any indication as to how to even start if I were to try to implement this in a fork ?

@JelleZijlstra
Copy link
Member

I think some work has been done since 2017 that should make it a bit easier. Look for TypeAliasType in the code.

@pranavrajpal
Copy link
Contributor

I've looked into this, and there are 2 issues that will probably make this somewhat hard to implement completely:

  1. There are a decent number of places where code calls get_proper_type on arguments and then passes on the expanded alias to another function, instead of the original alias, despite get_proper_type's docstring saying not to do that. That means that the error handling code sometimes only sees the expanded alias, so it doesn't have enough info to show the alias name in the error.
  2. There's currently an optimization where the types for type aliases are eagerly expanded for aliases to types that are relatively simple (see here for more details). That means that, again, the alias name isn't available to the error message printing code, so we're probably going to have to remove that optimization.

I should be able to create a PR soon that starts to implement this. So far, I'm just fixing examples of problem 1 everywhere I happen to find, and I'm leaving out a fix for problem 2 (removing that optimization doesn't seem hard but it definitely seems nontrivial).

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

No branches or pull requests

7 participants