Skip to content

cast gives cryptic error: Invalid type "self" #6862

Closed

Description

@trickeydan
  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from typing import *

Integer = NewType("Integer", int)

class Foo:

    _bar: Integer

    def __init__(self, val: int) -> None:
        self._val = val
        self._bar = cast(self._val, self._bar)
  • What is the actual behavior/output?
    type.py:11: error: Invalid type "self"
  • What is the behavior/output you expect?
    An error message that explains that you cannot cast to an instance of a type.
  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
    mypy 0.701
    python 3.7.3
  • What are the mypy flags you are using? (For example --strict-optional)
    mypy.ini is as follows.
[mypy]
mypy_path = stubs

warn_unused_ignores = True
warn_return_any = True

strict_optional = True
no_implicit_optional = True

disallow_any_unimported = True
#disallow_any_expr = True
#disallow_any_decorated = True
disallow_any_explicit = True
disallow_subclassing_any = True
disallow_any_generics = True

disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True

check_untyped_defs = True

Activity

  1. gvanrossum commented on May 18, 2019

    @gvanrossum
    Member

    I wonder if using ‘self.something’ in other places that need a type give the same error.

  2. ilevkivskyi commented on May 19, 2019

    @ilevkivskyi
    Member

    This is a duplicate of a more general issue #4030. I prefer to track this in "centralized" way, so closing this one.

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