Skip to content

Better error message for "Invalid type" #4030

Closed

Description

@ilevkivskyi

There several situations where a quite terse error message is given: Invalid type "__main__.T" -- this could be an attempt to use a variable as a type, attempt to use function as a type, unsupported forward reference, maybe some other. I propose to make this error message more specific, or add a note:

error: Invalid type "__main__.f"
note: Functions can't be used as a type

and

error: Invalid type "__main__.Alias"
note: Variables can't be used as a type

The second can be also useful if someone accidentally created a variable instead of an alias. In this case we can even append a link to docs, if we detect that variable has type Type[...].

Another similar terse error message is Invalid base class. We can say which base is invalid, this will be useful in case of multiple bases on the same line, and maybe also add a note in some cases explaining why it is invalid, like note: Union types can't be subclassed, so that it is clear what went wrong.

Activity

  1. self-assigned this
    on Sep 29, 2017
  2. gvanrossum commented on Jan 24, 2018

    @gvanrossum
    Member

    Also e.g.

    class C:
      A = int
    
    a: C.A  # error: Invalid type "_.C.A"
  3. ilevkivskyi commented on Jan 24, 2018

    @ilevkivskyi
    MemberAuthor

    This case is actually quite important, especially while #3494 (document variables vs aliases) is not fixed.

  4. ilevkivskyi commented on Feb 4, 2019

    @ilevkivskyi
    MemberAuthor

    A rare case where adding "Invalid type" would be actually helpful is the error message for using subscriptable metaclasses in type context, see #6313.

  5. ilevkivskyi commented on Feb 11, 2019

    @ilevkivskyi
    MemberAuthor

    A similar example where we should give a better error message is (recently closed) #2477

  6. ilevkivskyi commented on May 19, 2019

    @ilevkivskyi
    MemberAuthor

    One more example is cast targets, see #6862

  7. changed the title [-]Better error messge for "Invalid type"[/-] [+]Better error message for "Invalid type"[/+] on May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions