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

TypeError: truncate() takes no keyword arguments #58791

Closed
TheBiggerGuy mannequin opened this issue Apr 15, 2012 · 15 comments
Closed

TypeError: truncate() takes no keyword arguments #58791

TheBiggerGuy mannequin opened this issue Apr 15, 2012 · 15 comments
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@TheBiggerGuy
Copy link
Mannequin

TheBiggerGuy mannequin commented Apr 15, 2012

BPO 14586
Nosy @birkenfeld, @pitrou, @bitdancer, @berkerpeksag, @vadmium
Files
  • test.py: Test file
  • truncate.ver1.patch: First Version allowing both positional and keyword parameters
  • truncate.ver2.patch: Second Version (still needs work)
  • 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 = None
    closed_at = None
    created_at = <Date 2012-04-15.02:04:32.463>
    labels = ['interpreter-core', 'type-bug', '3.10', 'docs']
    title = 'TypeError: truncate() takes no keyword arguments'
    updated_at = <Date 2020-12-15.23:28:33.148>
    user = 'https://bugs.python.org/TheBiggerGuy'

    bugs.python.org fields:

    activity = <Date 2020-12-15.23:28:33.148>
    actor = 'iritkatriel'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'Interpreter Core']
    creation = <Date 2012-04-15.02:04:32.463>
    creator = 'TheBiggerGuy'
    dependencies = []
    files = ['25219', '25246', '25251']
    hgrepos = []
    issue_num = 14586
    keywords = ['patch']
    message_count = 14.0
    messages = ['158308', '158388', '158403', '158410', '158494', '158495', '158507', '158509', '158513', '158535', '158547', '158548', '244937', '251139']
    nosy_count = 7.0
    nosy_names = ['georg.brandl', 'pitrou', 'r.david.murray', 'docs@python', 'berker.peksag', 'martin.panter', 'TheBiggerGuy']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue14586'
    versions = ['Python 3.10']

    @TheBiggerGuy
    Copy link
    Mannequin Author

    TheBiggerGuy mannequin commented Apr 15, 2012

    The Python docs suggest that io.IOBase.truncate' should take a keyword argument of 'size'.
    However this causes a 'TypeError':
    TypeError: truncate() takes no keyword arguments

    Suggest that the docs are changed to 'truncate(size)' or CPython is changed to allow the keyword.

    http://docs.python.org/py3k/library/io.html?highlight=truncate#io.IOBase.truncate
    http://docs.python.org/library/io.html?highlight=truncate#io.IOBase.truncate

    @TheBiggerGuy TheBiggerGuy mannequin assigned docspython Apr 15, 2012
    @TheBiggerGuy TheBiggerGuy mannequin added docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 15, 2012
    @bitdancer
    Copy link
    Member

    The suggested doc change won't work, since that would imply that the size argument was required. We'd have to use the old truncate([size]) notation.

    Supporting it as a keyword argument is probably to be preferred, but someone will have to write the patch :)

    @TheBiggerGuy
    Copy link
    Mannequin Author

    TheBiggerGuy mannequin commented Apr 16, 2012

    What ever change is made to the new CPythons the old docs should be updated to prevent confusion, with truncate([size]).

    On fixing it for the future I would agree that supporting it as a keyword argument is preferred, as it is more pythonic (in my opinion). However this would cause ether backwards incompatibility or ambiguity in the language (ie. truncate(0, size=1) or need the deprecate, warn then removal stages taken three release cycles).

    Maybe the less perfect but acceptable solution is just to change the docs and wait for Python 4k for the real fix....?

    @bitdancer
    Copy link
    Member

    There wouldn't be serious backward incompatibility. Truncate(0) would still mean the same thing as truncate(size=0). I don't remember if we treat supporting the keyword form when it is doced that as a bug or not, though, so you might be right that it can't be backported.

    @TheBiggerGuy
    Copy link
    Mannequin Author

    TheBiggerGuy mannequin commented Apr 16, 2012

    @murray The thing I would be worried at in both supporting truncate(0) and truncate(size=0) would be truncate(0, size=1). This could throw an exception but causes the need for extra sanity checks and introduces ambiguity in the otherwise 'only one way to do something' Python style.

    @bitdancer
    Copy link
    Member

    I think you misunderstand the way that python arguments work. If you have a function so:

      def func(size=None):

    Then func(0) and func(size=0) are equivalent, and func(0, size=0) is a TypeError because you've provided two arguments instead of just one. The C code *can* emulate this, but often doesn't (it just accepts positional arguments instead). An issue was raised about changing most C functions to support the keyword syntax, but I believe it was decided that while in general doing so is good we'd only do it on a case by case basis as they came up. See bpo-8706 for more details.

    @TheBiggerGuy
    Copy link
    Mannequin Author

    TheBiggerGuy mannequin commented Apr 16, 2012

    Looking through cpython and trying to form a patch I found several differing interpretations of truncate:
    Lib/_pyio.py
      def truncate(self, pos=None):
    Modules/_io/fileio.c
      PyDoc_STRVAR(truncate_doc, "truncate([size: int]) ...");

    A first semi-working patch is attached. This will allow:
    truncate()
    truncate(x)
    truncate(size=x)
    and fail on:
    truncate(x, size=x)
    truncate(x, size=y)

    Thoughts?

    ps.
    fileio_truncate is defined as
    (PyCFunctionWithKeywords)fileio_truncate, METH_VARARGS | METH_KEYWORDS
    but fails with "takes no keyword arguments". How do I fix this?

    @TheBiggerGuy
    Copy link
    Mannequin Author

    TheBiggerGuy mannequin commented Apr 16, 2012

    Sorry had not refreshed the page to pick up the last comment. After reading more the cpython code I get what you are saying now. Not a fan of that syntax but consistency is best.
    This is my first time working with cpython directly, I have only worked on small python to c modules before so please say if I am barking up the wrong tree.

    @birkenfeld
    Copy link
    Member

    The patch is wrong: PyArg_ParseTupleAndKeywords already handles the correct assignment of positional and keyword args, and raises exceptions accordingly. Did you test that code?

    The question is also: why only truncate()? There are several other fileio_* methods that take VARARGS only.

    @TheBiggerGuy
    Copy link
    Mannequin Author

    TheBiggerGuy mannequin commented Apr 17, 2012

    @Brandl truncate() was the issue I ran into, no other reason. I have started on the rest of the IO module tho. I know the patch is not working but I ran into problems with getting cpython to change functions from positional to keyword.

    @ALL
    cpython | Python | Status
    -----------------------+-------------------------------+-------
    iobase_readlines | readline(limit=-1) | patch v2
    iobase_readline | readlines(hint=-1) | patch v2
    iobase_seek | seek(offset, whence=SEEK_SET) | patch v2
    iobase_truncate | truncate(size=None) | patch v2
    fileio_seek | seek(offset, whence=SEEK_SET) | patch v2
    fileio_read | read(n=-1) | patch v2
    textiowrapper_read | read(n=-1) | ToDo
    textiowrapper_readline | readline(limit=-1) | ToDo
    textiowrapper_seek | seek(offset, whence=SEEK_SET) | ToDo
    textiowrapper_truncate | truncate(size=None) | ToDo
    textiobase_read | read(n=-1) | ToDo
    textiobase_readline | readline(limit=-1) | ToDo
    {bufferedio.c} | | ToDo
    {bytesio.c} | | ToDo
    {stringio.c} | | ToDo

    ps.
    I am using code from within other C files and http://docs.python.org/dev/extending/extending.html#keyword-parameters-for-extension-functions to base my patch on but I still get "x()" takes no keyword arguments". What have I missed/Are the docs correct?

    @bitdancer
    Copy link
    Member

    Ah, I'm glad someone else chimed in. I was going to say that I was pretty sure we had a macro for doing this, but I don't do much C level coding so I didn't have a reference handy.

    @bitdancer
    Copy link
    Member

    macro, function...something automated, anyway :)

    @vadmium
    Copy link
    Member

    vadmium commented Jun 7, 2015

    See also bpo-23738 and PEP-457 for fixing the documentation instead, possibly something like truncate(size=None, /). Also, bpo-17003 has changed some of the keywords to be more consistent, making parts of this patch incorrect.

    @vadmium
    Copy link
    Member

    vadmium commented Sep 20, 2015

    I agree with Guy’s earlier comments and would prefer this be fixed in the documentation. Otherwise, we would end up with third party IOBase implementations that use the wrong keyword name, or that don’t accept keywords at all. These would no longer be compatible with the new API.

    Also the new patch competes with bpo-25057, also proposing keyword arguments for seek().

    @iritkatriel iritkatriel added the 3.10 only security fixes label Dec 15, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel removed the 3.10 only security fixes label May 6, 2022
    @slateny
    Copy link
    Contributor

    slateny commented Jun 1, 2022

    Fixed per #91950

    @slateny slateny closed this as completed Jun 1, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir 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