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

int() can return not exact int instance #71171

Closed
serhiy-storchaka opened this issue May 9, 2016 · 7 comments
Closed

int() can return not exact int instance #71171

serhiy-storchaka opened this issue May 9, 2016 · 7 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 26984
Nosy @mdickinson, @serhiy-storchaka
Files
  • int_exact.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2016-08-21.17:17:05.231>
    created_at = <Date 2016-05-09.14:02:31.612>
    labels = ['interpreter-core', 'type-bug']
    title = 'int() can return not exact int instance'
    updated_at = <Date 2016-08-21.17:17:05.231>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2016-08-21.17:17:05.231>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-08-21.17:17:05.231>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2016-05-09.14:02:31.612>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['43600']
    hgrepos = []
    issue_num = 26984
    keywords = ['patch']
    message_count = 7.0
    messages = ['265196', '265210', '269663', '270626', '273057', '273291', '273305']
    nosy_count = 3.0
    nosy_names = ['mark.dickinson', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26984'
    versions = ['Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

    The int constructor can return an instance of int subclass.

    >>> class BadTrunc:
    ...     def __trunc__(self):
    ...         return True
    ... 
    >>> int(BadTrunc())
    True

    When __int__ returns non-exact int, at least a warning is emitted:

    >>> class BadInt:
    ...     def __int__(self):
    ...         return True
    ... 
    >>> int(BadInt())
    __main__:1: DeprecationWarning: __int__ returned non-int (type bool).  The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
    True

    The constructor of int subclass always return an instance of correct type:

    >>> class IntSubclass(int):
    ...     pass
    ... 
    >>> type(IntSubclass(BadTrunc()))
    <class '__main__.IntSubclass'>
    >>> type(IntSubclass(BadInt()))
    __main__:1: DeprecationWarning: __int__ returned non-int (type bool).  The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
    <class '__main__.IntSubclass'>

    I don't know if it is worth to deprecate __trunc__ returning non-exact int, since this special method is used in math.trunc(). But I think that the int constructor should convert its result to exact int. If some preparatory period is needed, it can first start to emit FutureWarning.

    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels May 9, 2016
    @mdickinson
    Copy link
    Member

    But I think that the int constructor should convert its result to exact int.

    Agreed. See also bpo-17576 and previous discussions on python-dev (thread started in March, but most of the messages in April):

    I don't have strong feelings about __trunc__ (or __floor__ and __ceil__); it seems fine to me for those to be returning something other than a strict int.

    @serhiy-storchaka
    Copy link
    Member Author

    Proposed patch makes int() always returning exact int.

    @serhiy-storchaka
    Copy link
    Member Author

    Could you please make a review Mark?

    @mdickinson mdickinson self-assigned this Aug 18, 2016
    @mdickinson
    Copy link
    Member

    Could you please make a review Mark?

    Sorry, Serhiy. I missed this. I've got some time off coming up, so I plan to look at this in the next few days.

    @mdickinson
    Copy link
    Member

    The patch LGTM. I'm still in two minds about whether __trunc__ should be required to return a strict int, but for now it's probably better to go with the status quo.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 21, 2016

    New changeset 81f229262921 by Serhiy Storchaka in branch 'default':
    Issue bpo-26984: int() now always returns an instance of exact int.
    https://hg.python.org/cpython/rev/81f229262921

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants