Skip to content

Can't cast from Fraction to int? #9588

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

Closed
jvdwetering opened this issue Oct 13, 2020 · 8 comments · Fixed by python/typeshed#4665
Closed

Can't cast from Fraction to int? #9588

jvdwetering opened this issue Oct 13, 2020 · 8 comments · Fixed by python/typeshed#4665
Labels
bug mypy got something wrong

Comments

@jvdwetering
Copy link

jvdwetering commented Oct 13, 2020

In mypy 0.770 the following typechecks:

from fractions import Fraction

def f(a: Fraction) -> int:
	return int(a)

But it raises the following error in mypy 0.790: "error: No overload variant of "int" matches argument type "Fraction""

If I replace the last line with return int(float(a)), it does typecheck.

Is this intended behaviour?

EDIT: The code above actually also doesn't compile on 0.770, see below on modified code that does work on 0.770 but gives an error on 0.790

@jvdwetering jvdwetering added the bug mypy got something wrong label Oct 13, 2020
@hauntsaninja
Copy link
Collaborator

This fails for me with 0.770 as well. I assume this is similar to #3186

@jvdwetering
Copy link
Author

My bad, I accidentally had it ignoring the error in my 0.770 installation. But similar code I had in a more complicated code base did parse with 0.770, but started to raise errors when I upgraded mypy to 0.790 (even though I did not change anything about the codebase).

And yeah, this definitely looks to be related to #3186.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 13, 2020

Regressions are typically easier to fix, so if you find the failure that would be useful. Could be a problem in python/typeshed#4401 if that made it into the release

@jvdwetering
Copy link
Author

jvdwetering commented Oct 13, 2020

Okay, I found a variant of my code above that does work in 0.770 but not in 0.790:

from fractions import Fraction

class Foo(object):
	def __init__(self,a: Fraction):
		self.a = a

def f(bar: Foo) -> int:
	return int(bar.a*2)

Removing the *2 also breaks it in 0.770

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 13, 2020

Thanks! Okay, this is triggered by python/typeshed#4401 (but isn't a bug). That PR adds a return type for Fraction's __mul__, where previously it returned Any (since the stubs were incomplete).
This causes the underlying error to surface, since mypy now knows that bar.a * 2 is a Fraction.

@jvdwetering
Copy link
Author

That makes sense. So now the remaining problem is that int() should really know how to deal with a Fraction, right?

@hauntsaninja
Copy link
Collaborator

Yeah. From a glance, I'm not actually sure how this is even accomplished in CPython since Fraction doesn't have __int__, but will take a look later.

@hauntsaninja
Copy link
Collaborator

Here you go, this should fix things: python/typeshed#4665

srittau pushed a commit to python/typeshed that referenced this issue Oct 14, 2020
jlaine added a commit to jlaine/aiortc that referenced this issue Oct 14, 2020
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
jlaine added a commit to jlaine/aiortc that referenced this issue Oct 14, 2020
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
jlaine added a commit to aiortc/aiortc that referenced this issue Oct 14, 2020
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
PAN-Chuwen pushed a commit to PAN-Chuwen/StreamPose that referenced this issue Sep 15, 2023
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
17852833820 pushed a commit to 17852833820/AioRTC that referenced this issue Jan 20, 2024
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
mametaro99 pushed a commit to mametaro99/image-recognition that referenced this issue May 11, 2024
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
michael-angelo-guban pushed a commit to michael-angelo-guban/python-arotic that referenced this issue Apr 12, 2025
Otherwise it fails with:

src/aiortc/mediastreams.py:22: error: No overload variant of "int" matches argument type "Fraction"
src/aiortc/mediastreams.py:22: note: Possible overload variant:
src/aiortc/mediastreams.py:22: note:     def __init__(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
src/aiortc/mediastreams.py:22: note:     <1 more non-matching overload not shown>

See: python/mypy#9588
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants