Skip to content

Same name for instance and type object may be confusing #3372

Closed

Description

@elazarg

A type object and an instance of that type are often printed in the same way:

$ mypy -c'True.x'
<string>:1: error: "bool" has no attribute "x"
$ mypy -c'bool.x'
<string>:1: error: "bool" has no attribute "x"

Most of the time this is OK, although when metaclasses are involved it becomes confusing. But even without metaclasses, simply forgetting to instantiate a class results in an error that does not make sense:

class A: pass
class B(A): pass
def f(a: A): pass

f(B)  # E: Argument 1 to "f" has incompatible type "B"; expected "A"
a: A = B  # E: Incompatible types in assignment (expression has type "B", variable has type "A")

But of course "B" is compatible with "A". It's the type object "B" that is incompatible with "A".

In the above example, passing A to f gives a more explicit error saying that "B" is a type object, but this is done while trying to give distinct name for each type, so this does not solve the general problem.

Activity

  1. refi64 commented on May 16, 2017

    @refi64
    Contributor

    I think this is a dup? I recall a very similar issue coming up before...

  2. elazarg commented on May 16, 2017

    @elazarg
    ContributorAuthor

    I can't find it :\

  3. ilevkivskyi commented on May 16, 2017

    @ilevkivskyi
    Member

    @elazarg
    Here is the issue #3050 could you please copy your example there? (I am closing this as a duplicate)

  4. elazarg commented on May 16, 2017

    @elazarg
    ContributorAuthor

    Yes I just found it... I will. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions