Skip to content

Incompatibles types in assignment #5765

Closed
@remdragon

Description

@remdragon
  • Are you reporting a bug, or opening a feature request?
This looks like a bug to me
  • 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 __future__ import print_function

import builtins
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from six import text_type as unicode # pip install six

if not getattr ( builtins, 'long', None ):
	long = int

MYPY = False
if MYPY:
	from typing import List, Optional, Text, Union
	VALUE = Union[int,long,float,Decimal,unicode,datetime,date,time,timedelta]

class Variant ( object ):
	name = None # type: Text
	value = None # type: Optional[VALUE]
	
	def __init__ ( self, name, value ):
		# type: ( Text, Optional[VALUE] ) -> None
		self.name = name
		self.value = value

def GetParams():
	# type: () -> List[Variant]
	return [ Variant ( u'Foo', u'Bar' ) ]

params = [] # type: List[Union[Variant,VALUE]]

params = GetParams() # THIS IS THE LINE THAT THROWS THE ERROR

for param in params:
	if isinstance ( param, Variant ):
		print ( param.value )
	else:
		print ( repr ( param ) )
  • What is the actual behavior/output?
error: Incompatible types in assignment (expression has type "List[Variant]", variable has type "List[Union[Variant, int, int, float, Decimal, unicode, datetime, date, time, timedelta]]")
  • What is the behavior/output you expect?
I would expect that I should be able to assign an expression of type List[Variant] to a variable of type List[Union[Variant,VALUE]]
  • 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.630
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)] on win32
I have not tried git master. I'm terribly sorry, but I unfortunately don't have the time to try to setup an environment where I can test with git master.
  • What are the mypy flags you are using? (For example --strict-optional)
py2: --ignore-missing-imports --strict --py2
py3: --ignore-missing-imports --strict

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