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

Mixing python functions and predicate objects raises error #5698

Closed
Achimh3011 opened this issue Sep 30, 2018 · 4 comments
Closed

Mixing python functions and predicate objects raises error #5698

Achimh3011 opened this issue Sep 30, 2018 · 4 comments

Comments

@Achimh3011
Copy link

A Callable and a predicate object implementing the same behavior should be assignable to the same variable without mypy raising an error.

  • 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.

https://gist.github.com/Achimh3011/262acb8bf6d1b61ee7da6c574b662bd1

  • What is the actual behavior/output?
sample.py:39: error: Incompatible types in assignment (expression has type "Callable[[Path], bool]", variable has type "matching_suffix")
  • What is the behavior/output you expect?

no error

  • What are the versions of mypy and Python you are using?
mypy-extensions==0.4.1
typed-ast==1.1.0
  • Do you see the same issue after installing mypy from Git master?

yes

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

none

@JelleZijlstra
Copy link
Member

You probably need to pre-declare file_filter (e.g., with file_filter: Callable[[Path], bool]). Mypy basically goes with the first assignment otherwise.

@ilevkivskyi
Copy link
Member

This is not a bug. matching_suffix is a narrower type than just Callable[[Path], bool] (for example you can access suffixes on it). You therefore need to either give an annotation or switch order of branches.

To clarify, this is an error from mypy's point of view:

if cond():
    x = 'hi'
else:
    x = 42

@ilevkivskyi
Copy link
Member

Also FYI there is an existing proposal to allow this and automatically infer a union #2611

@Achimh3011
Copy link
Author

Thx for the hint with the pre-declaration and the link to #2611.

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