Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible types in assignment for property setter #8348

Closed
tamuhey opened this issue Jan 31, 2020 · 6 comments
Closed

Incompatible types in assignment for property setter #8348

tamuhey opened this issue Jan 31, 2020 · 6 comments

Comments

@tamuhey
Copy link

tamuhey commented Jan 31, 2020

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • 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 List, Iterable

class Foo:
    @property
    def x(self) -> List[int]:
        ...

    @x.setter
    def x(self, new_x: Iterable[int]):
        ...

foo = Foo()
foo.x = (1, 2, 3) # error: Incompatible types in assignment (expression has type "Tuple[int, int, int]", variable has type "List[int]")
  • What is the actual behavior/output?
error: Incompatible types in assignment (expression has type "Tuple[int, int, int]", variable has type "List[int]")
  • What is the behavior/output you expect?

No error because setter requires Iterable[int], not List[int]

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

master

  • What are the mypy flags you are using? (For example --strict-optional)

no flag

  • If mypy crashed with a traceback, please paste
    the full traceback below.
foo.py:13: error: Incompatible types in assignment (expression has type "Tuple[int, int, int]", variable has type "List[int]")
Found 1 error in 1 file (checked 1 source file)
@bsdtux
Copy link

bsdtux commented Jan 31, 2020

We are having a similar issue. We are taking a str_date and a format and trying to do a strptime
but we keep getting the error of

error: Incompatible types in assignment (expression has type "datetime", variable has type "str")

The below section of code is where we are getting the error from

date_formats = ['%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d']
event_dates = {
        "date_1": event_date_1,
        "date_2": event_date_2
    }
for key, date in event_dates.items():
    for date_format in date_formats:
        try:
            date = datetime.strptime(date, date_format) # This is the offending line
            if date < datetime.strptime('1900-01-01', '%Y-%m-%d'):
                event_dates[key] = None
        except (ValueError, TypeError):
            pass

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 1, 2020

@bsdtux I think your issue is unrelated; pretty sure you're running into https://mypy.readthedocs.io/en/stable/common_issues.html#redefinitions-with-incompatible-types. You might be able to get around it by renaming the variable (to, eg, date_str) or using --allow-redefinition

@bsdtux
Copy link

bsdtux commented Feb 3, 2020

@bsdtux I think your issue is unrelated; pretty sure you're running into https://mypy.readthedocs.io/en/stable/common_issues.html#redefinitions-with-incompatible-types. You might be able to get around it by renaming the variable (to, eg, date_str) or using --allow-redefinition

thanks @hauntsaninja it looks like what it was upset about was that we were taking the date variable in the loop and going from a string to a date object. So after taking your advice we change the date in the for loop to be date_str and the date variable holding the output of strptime was changed to new_date and now mypy is happy.

@bsdtux
Copy link

bsdtux commented Feb 3, 2020

@tamuhey I wonder if yours is the same where you define the x property of having a list type but then on the setter, you are saying it takes an iterable. What happens if you set the getter for x to be type iterable as well?

@tamuhey
Copy link
Author

tamuhey commented Feb 3, 2020

What happens if you set the getter for x to be type iterable as well?

The users of Foo must rewrite their code. List is assinable to Iterable but Iterable is not assinable to List.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 6, 2020

This is a duplicate of #3004 (also linking #8084 and #220)

@tamuhey tamuhey closed this as completed Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants