Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

mypy failed to narrow down union types includingSelf withisinstance #14912

Closed
@mon-jai

Description

@mon-jai

Bug Report

mypy failed to narrow down union types includingSelf withisinstance.

To Reproduce

Consider the following code:

fromdataclassesimportdataclassfromtypingimportSelf,reveal_type@dataclassclassFoo:i:intdefbar(self,other:Self|int)->int:ifisinstance(other,Foo):returnself.i*other.ielse:reveal_type(other)# "int" expected, gets "Self | int"returnself.i*other# Unsupported operand types for * ("int" and "Self")  [operator]mypy(error)

Expected Behavior

Other should be infered as anint and mypy reports no error.

Actual Behavior

mypy failed to narrow type ofother

Revealed type is "Union[Self`0, builtins.int]"mypy(note)

Also, mypy reports the following error.

Unsupported operand types for * ("int" and "Self")  [operator]mypy(error)

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags:--follow-imports=silent --ignore-missing-imports --show-column-numbers --no-pretty (VSCode defaults)
  • Mypy configuration options frommypy.ini (and other config files): None
  • Python version used: 3.11.1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp