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

work around awkward situation with float subclasses #528

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jesboat
Copy link

@jesboat jesboat commented Nov 5, 2021

Summary:

On python before 3.9, float is a subclass of the ABC numbers.Real which
defines abstract methods __floor__ and __ceil__, but doesn't provide
an actual implementation of those methods. This is arguably a type error
in Python, because the following code is well-typed:

def foo(x: numbers.Real): return x.__floor()
def bar(x: float): return foo(x)
bar(some float)

but fail at runtime. This doesn't create a user-visible problem because
__floor__ and __ceil__ aren't meant to be called directly; instead,
users are supposed to call math.floor(x) and math.ceil(x) which are
fine without the dunder methods as long as the object is a primitive
number (or a subclass of one, or can be converted to one in various
ways.)

This creates the unfortunate situation that Pyre won't let people
instantiate subclasses of float without definitions of __floor__ and
__ceil__, even though no typical code actually requires those methods.
(Pyre currently has a special case to allow the instantiation of float
itself.)

The ideal behavior would be if Pyre treated numbers.Real as "in order
to be a subtype of numbers.Real, you need to implement the floor/ceil
dunder methods, or be a subtype of int/float/etc, or implement
__float__ or __index__, etc." and "numbers.Real are not guaranteed
to define __floor__ and __ceil__. This would be insane to implement.

Pretending that float defines __floor__ and __ceil__ is a
minimally objectionable workaround. It means that float will continue to
work, subclasses of float will start to work, and the only well-typed
code which can error at runtime is val.__floor__() when
type(val) == T <: float... But that problem already exists when
T == float and so I don't find extending it to subclasses of float to
be particularly concerning.

Resolves #527 (github)

Test plan:

These two examples fail to typecheck before:

# example 1
import typing
T = typing.NewType('T', float)
T(4.0)

# example 2
class C(float):
    def __init__(self, v: float) -> None: pass
C(4.0)

with errors:

example.py:3:0 Invalid class instantiation [45]:
    Cannot instantiate abstract class `T` with abstract methods `__ceil__`, `__floor__`.

example.py:9:14 Invalid class instantiation [45]:
    Cannot instantiate abstract class `C` with abstract methods `__ceil__`, `__floor__`.

but both pass after.

Summary:

On python before 3.9, `float` is a subclass of the ABC numbers.Real which
defines abstract methods `__floor__` and `__ceil__`, but doesn't provide
an actual implementation of those methods. This is arguably a type error
in Python, because the following code is well-typed:

    def foo(x: numbers.Real): return x.__floor()
    def bar(x: float): return foo(x)
    bar(some float)

but fail at runtime. This doesn't create a user-visible problem because
`__floor__` and `__ceil__` aren't meant to be called directly; instead,
users are supposed to call `math.floor(x)` and `math.ceil(x)` which are
fine without the dunder methods as long as the object is a primitive
number (or a subclass of one, or can be converted to one in various
ways.)

This creates the unfortunate situation that Pyre won't let people
instantiate subclasses of `float` without definitions of `__floor__` and
`__ceil__`, even though no typical code actually requires those methods.
(Pyre currently has a special case to allow the instantiation of `float`
itself.)

The ideal behavior would be if Pyre treated `numbers.Real` as "in order
to be a subtype of `numbers.Real`, you need to implement the floor/ceil
dunder methods, or be a subtype of int/float/etc, or implement
`__float__` or `__index__`, etc." and "numbers.Real are not guaranteed
to define `__floor__` and `__ceil__`. This would be insane to implement.

Pretending that `float` defines `__floor__` and `__ceil__` is a
minimally objectionable workaround. It means that float will continue to
work, subclasses of float will start to work, and the only well-typed
code which can error at runtime is `val.__floor__()` when
`type(val) == T <: float`... But that problem already exists when
`T == float` and so I don't find extending it to subclasses of float to
be particularly concerning.

Resolves facebook#527 (github)

Test plan:

These two examples fail to typecheck before:

    # example 1
    import typing
    T = typing.NewType('T', float)
    T(4.0)

    # example 2
    class C(float):
        def __init__(self, v: float) -> None: pass
    C(4.0)

with errors:

    example.py:3:0 Invalid class instantiation [45]:
        Cannot instantiate abstract class `T` with abstract methods `__ceil__`, `__floor__`.

    example.py:9:14 Invalid class instantiation [45]:
        Cannot instantiate abstract class `C` with abstract methods `__ceil__`, `__floor__`.

but both pass after.
@facebook-github-bot
Copy link
Contributor

Hi @jesboat!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot
Copy link
Contributor

@arthaud has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@jesboat
Copy link
Author

jesboat commented Nov 5, 2021

Out of curiosity, did the Summary/Test Plan in my commit message end up in the right phabricator fields?

@arthaud
Copy link
Contributor

arthaud commented Nov 5, 2021

Yes it did :)

@arthaud
Copy link
Contributor

arthaud commented Nov 8, 2021

Question from @grievejia: could we fix this in typeshed directly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot instantiate subclasses (including newtypes) of floats
3 participants