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

Change object.__format__(s) where s is non-empty to a TypeError #54065

Closed
ericvsmith opened this issue Sep 14, 2010 · 21 comments
Closed

Change object.__format__(s) where s is non-empty to a TypeError #54065

ericvsmith opened this issue Sep 14, 2010 · 21 comments
Assignees
Labels
deferred-blocker easy interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ericvsmith
Copy link
Member

BPO 9856
Nosy @terryjreedy, @ericvsmith, @bitdancer, @asvetlov, @florentx, @serhiy-storchaka
Files
  • issue9856_python-3.4.diff
  • 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/ericvsmith'
    closed_at = <Date 2013-05-19.13:11:53.900>
    created_at = <Date 2010-09-14.17:26:57.550>
    labels = ['interpreter-core', 'easy', 'deferred-blocker', 'type-bug']
    title = 'Change object.__format__(s) where s is non-empty to a TypeError'
    updated_at = <Date 2014-02-11.23:34:43.381>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2014-02-11.23:34:43.381>
    actor = 'python-dev'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2013-05-19.13:11:53.900>
    closer = 'r.david.murray'
    components = ['Interpreter Core']
    creation = <Date 2010-09-14.17:26:57.550>
    creator = 'eric.smith'
    dependencies = []
    files = ['23936']
    hgrepos = []
    issue_num = 9856
    keywords = ['patch', 'easy']
    message_count = 21.0
    messages = ['116413', '116751', '130681', '130682', '149351', '177970', '177977', '177981', '177983', '177984', '177992', '177994', '189048', '189049', '189062', '189068', '189089', '189107', '189586', '205017', '211041']
    nosy_count = 11.0
    nosy_names = ['terry.reedy', 'eric.smith', 'Arfrever', 'r.david.murray', 'asvetlov', 'flox', 'python-dev', 'serhiy.storchaka', 'krinart', 'Ankur.Ankan', 'Yogesh.Chaudhari']
    pr_nums = []
    priority = 'deferred blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue9856'
    versions = ['Python 3.4']

    @ericvsmith
    Copy link
    Member Author

    In 3.3 the existing PendingDeprecationWarning needs to become a DeprecationWarning. In 3.4 it will become an error.

    I'll change 3.3 after 3.2 is released.

    See bpo-7994 for the original PendingDeprecationWarning discussion.

    @ericvsmith ericvsmith self-assigned this Sep 14, 2010
    @ericvsmith ericvsmith added interpreter-core (Objects, Python, Grammar, and Parser dirs) easy type-bug An unexpected behavior, bug, or error labels Sep 14, 2010
    @terryjreedy
    Copy link
    Member

    Perhaps this could be expanded to "Deprecation warnings in 3.3" with release-blocker priority to list all 3.2 PendingDWs. Once changes are made, it could be retitled "Removals in 3.4", with a new "Deprecation warnings in 3.4" added.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 12, 2011

    New changeset ee259a4f3eee by Eric V. Smith in branch 'default':
    bpo-9856: Change object.__format__ with a non-empty format string from a PendingDeprecationWarning to a DeprecationWarning.
    http://hg.python.org/cpython/rev/ee259a4f3eee

    @ericvsmith
    Copy link
    Member Author

    Next step is to make it a TypeError in 3.4.

    @ericvsmith ericvsmith changed the title Change object.__format__(s) where s is non-empty to a DeprecationWarning Change object.__format__(s) where s is non-empty to a TypeError Mar 12, 2011
    @florentx
    Copy link
    Mannequin

    florentx mannequin commented Dec 12, 2011

    Patch is ready for python 3.4

    :-)

    @krinart
    Copy link
    Mannequin

    krinart mannequin commented Dec 23, 2012

    As I can see this is already implemented in 3.4

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 23, 2012

    New changeset d91c14788729 by Andrew Svetlov in branch 'default':
    Issue bpo-9856: Replace deprecation warinigs to raising TypeError in object.__format__
    http://hg.python.org/cpython/rev/d91c14788729

    @asvetlov
    Copy link
    Contributor

    Committed. Thanks.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 23, 2012

    New changeset 2f6ec67636b8 by Andrew Svetlov in branch 'default':
    Add NEWS and docs for bpo-9856
    http://hg.python.org/cpython/rev/2f6ec67636b8

    @asvetlov
    Copy link
    Contributor

    Updated NEWS and docs

    @ericvsmith
    Copy link
    Member Author

    The more I think about this, the more overly restrictive I realize it is. If the type of the object really is "object", then it can use string formatting. It's only for non-objects that I want to add the error.

    I'll re-open it and give it some more thought.

    @ericvsmith ericvsmith reopened this Dec 23, 2012
    @asvetlov
    Copy link
    Contributor

    Ok

    @YogeshChaudhari
    Copy link
    Mannequin

    YogeshChaudhari mannequin commented May 12, 2013

    @eric:
    when you say: "If the type of the object really is "object", then it can use string formatting. It's only for non-objects that I want to add the error.".

    I am confused. Let me demonstrate what I'm thinking according to the statement above.

    First let us take a 'non-object':
    >>> integer=1
    >>> type(integer) != object
    True
    As of now it returns the following:
    >>> integer.__format__(s)
    '1'
    Which seems natural.
    But after this patch should it return an error
    
    Also now consider an object:
    >>> f = object()
    >>> type(f)
    <class 'object'>
    This will return the following after the patch as it does now which is:
    >>> f.__format__('')
    '<object object at 0xb75b7b48>'

    Does this mean that 'integer' should give an error, however, 'f' should give something that appears messy?

    I may be mistaken in my interpretation of the statement, so kindly let me know if there is something else that I am not clearly understanding.

    @YogeshChaudhari
    Copy link
    Mannequin

    YogeshChaudhari mannequin commented May 12, 2013

    Please replace
    integer.__format__(s)
    with
    integer.__format__('')

    @ericvsmith
    Copy link
    Member Author

    But int has its own __format__ method, so this does not apply. Per the title of this issue, this only refers to object.__format__.

    For example:

    This works now, and will continue working:
    >>> format(2, '1')
    '2'
    
    This is currently an error, and will remain an error:
    >>> class C: pass
    ...
    >>> format(C(), '1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: non-empty format string passed to object.__format__
    
    It's this case that is currently an error, but it need not be:
    >>> format(object(), '1')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: non-empty format string passed to object.__format__

    The more I think about it, the more I think it would be too confusing to make object.__format__ behave differently if self is of type object, versus another type. So I'll probably just close this as fixed unless someone feels strongly about it.

    @YogeshChaudhari
    Copy link
    Mannequin

    YogeshChaudhari mannequin commented May 12, 2013

    >It's this case that is currently an error, but it need not be:
    >>>> format(object(), '1')
    >Traceback (most recent call last):
    >  File "<stdin>", line 1, in <module>
    >TypeError: non-empty format string passed to object.__format__
    I believe that should continue to remain an error. Please note that this works
    >>> format(object(), '')
    '<object object at 0xb74fd688>'

    From what I can tell, specifying '1' or '2' or '100' makes no sense because unlike string or int (and like list or tuple ) this 'number' does not represent anything sensible.

    This works fine as it should:
    >>> format('q', '5')
    'q    '
    >>> format(1, '5')
    '    1'
    >>> format(1, '05')
    '00001'
    >>> 
    
    But this does not and IMHO *should* not 'work'
    >>> format([1,2,3], '5')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: non-empty format string passed to object.__format__
    >>> format((1,2,3), '5')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: non-empty format string passed to object.__format__

    an object() CANNOT have specific behavior like str or int. You can of-course argue as to what kind of error/exception/warning this may raise, but it does not make any sense (AFAIK) to 'fix' this.

    @terryjreedy
    Copy link
    Member

    Everying is an instance of object. If its class does not override .__format__, then it seems that it should act the same as a direct object instance. If this a the current plan (or patch already committed, I think I would stay with that.

    @YogeshChaudhari
    Copy link
    Mannequin

    YogeshChaudhari mannequin commented May 13, 2013

    +1 to Terry for "If its class does not override .__format__, then it seems that it should act the same as a direct object instance"

    @bitdancer
    Copy link
    Member

    Since Eric indicated he'd close this unless someone felt strongly that the status quo should be changed, and the arguments are in favor of *maintaining* the status quo, I'm going to close this for him :)

    @serhiy-storchaka
    Copy link
    Member

    Perhaps the versionchanged tag for format() is more suitable than versionadded.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 11, 2014

    New changeset f56b98143792 by R David Murray in branch 'default':
    whatsnew: object.__format__ raises TypeError on non-empty string.
    http://hg.python.org/cpython/rev/f56b98143792

    @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
    deferred-blocker easy 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

    5 participants