python / cpython

The Python programming language
https://www.python.org
Other
63.35k stars 30.32k forks source link

Unify tests for str.format and string.Formatter #58227

Open ncoghlan opened 12 years ago

ncoghlan commented 12 years ago
BPO 14019
Nosy @terryjreedy, @ncoghlan, @vstinner, @ericvsmith, @ezio-melotti, @merwok, @nanjekyejoannah

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: ```python assignee = None closed_at = None created_at = labels = ['type-feature', 'tests', '3.10'] title = 'Unify tests for str.format and string.Formatter' updated_at = user = 'https://github.com/ncoghlan' ``` bugs.python.org fields: ```python activity = actor = 'vstinner' assignee = 'none' closed = False closed_date = None closer = None components = ['Tests'] creation = creator = 'ncoghlan' dependencies = [] files = [] hgrepos = [] issue_num = 14019 keywords = ['patch'] message_count = 19.0 messages = ['153392', '153729', '154000', '154002', '194709', '195061', '196074', '217757', '217904', '217927', '218159', '218163', '218976', '219011', '219508', '348636', '380590', '380625', '380665'] nosy_count = 10.0 nosy_names = ['terry.reedy', 'ncoghlan', 'vstinner', 'eric.smith', 'ezio.melotti', 'eric.araujo', 'francismb', 'moijes12', 'francisco.freire', 'nanjekyejoannah'] pr_nums = [] priority = 'normal' resolution = None stage = 'patch review' status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue14019' versions = ['Python 3.10'] ```

ncoghlan commented 12 years ago

A couple of issues have arisen where features were added to str.format without similarly being added to string.Formatter.

This is only possible because the test cases for the two are currently almost entirely separate.

A common set of tests defined as (fmt, args, kwargs, output) tuples would help ensure that the implementations remained consistent in the future.

merwok commented 12 years ago

(ISTM that such test improvements would be beneficial to all branches, and we would also eschew merge issues if we change all branches. Other core devs sometimes object to test improvements or additions in stable branches though.)

fa53c3d6-e97b-4089-8725-a7655c5e9dde commented 12 years ago

I have some questions about this:

1) In Lib/test/string_tests.py it says: “Common tests shared by test_str, test_unicode, test_userstring and test_string”

but

a) I cannot find test_str

b) string is imported and only some constants ascii_letters and digits are used

c) In test_join there is a comment “see the test in test.test_string.StringTest.test_join” Is that obsolete? (I cannot find StringTest in the test_string test)

2) Is there more tests for the build in format (in test_buildin.BuiltinTest.test_format only the basic machinery is tested. I would expect something like in test_format.py somewhere

3) it is true that all tests for the build in 'format' should also pass in 'string.Formatter().format'

Thanks in advance!

francis

ncoghlan commented 12 years ago

At least the first couple of those look like obsolete comments left over from the 2.x branch (we didn't do a mass renaming in the test suite, so many tests still live in their old locations).

f20c269e-49c0-4d6c-bc70-7b263b8ade32 commented 11 years ago

I increased the coverage of formatter module to 40%. I added a new file "test_formatter.py" including some unit tests.

ezio-melotti commented 11 years ago

I left a review on rietveld.

FWIW I think string_tests and related files should undergo a (major?) refactoring. I worked with them a few times and it's a bit of a mess with all those base classes and mixins. I also found some tests that weren't running because they were accidentally overridden by one of the subclasses, or other tests that were duplicated. This is especially true on Python 3, where bytes and str share a smaller subset of features.

f20c269e-49c0-4d6c-bc70-7b263b8ade32 commented 11 years ago

Thanks for the review. I corrected some issues in my code. Here is the new patch.

fa53c3d6-e97b-4089-8725-a7655c5e9dde commented 10 years ago

The formatter module was deprecated in Python 3.4 and is scheduled for removal in Python 3.6. See [1] and [2].

--- [1] https://docs.python.org/3/library/formatter.html#module-formatter [2] https://docs.python.org/3/whatsnew/3.4.html#deprecated

ncoghlan commented 10 years ago

Note that this issue wasn't about the formatter module - it relates to the str.format() method and the string.Formatter *class*.

The formatter module is completely unrelated.

ericvsmith commented 10 years ago

The issue is about tests for str.format and string.Formatter, but in http://bugs.python.org/msg194709 and the associated patch there are tests for the formatter module to increase its coverage.

I suggested on the mentorship list that we break this into two issues.

brettcannon commented 10 years ago

Francisco, can you sign the contributor agreement? https://www.python.org/psf/contrib/contrib-form/

brettcannon commented 10 years ago

I added some review comments.

brettcannon commented 10 years ago

Set to "pending" while I wait to hear back from Francisco on the review comments.

f20c269e-49c0-4d6c-bc70-7b263b8ade32 commented 10 years ago

Hi, I signed the contributor agreement. Thank you for your review comments. I did these tests about one year ago and right now I don't have much time to look at it again. I hope to do so in the next months.

f6b1112c-a710-4ed6-bc6a-95202e665ef8 commented 10 years ago

Note that this issue wasn't about the formatter module - it relates to the str.format() method and the string.Formatter *class*.

I would tend to agree with Nick and Eric. From what I see in the patch, the tests are for formatter module and not the string.Formatter class.

vstinner commented 5 years ago

This issue is no newcomer friendly, I remove the "easy" keyword.

vstinner commented 3 years ago

@Irit Katriel: Please avoid changing the versions field of old inactive issues. It changes artificially the last activity date of an issue and it sends an email to all people in the nosy list. There is no need to update the Version field of the +7500 open bugs.

terryjreedy commented 3 years ago

Victor: Irit is reviewing old issues to decide whether to close or not. She has to touch it somehow to mark it as reviewed.

Irit: if you only change the version, others may think that you blindly updated the version. Better to say something that moves the issue forward. Also, only marking for the next version, now 3.10, would delay the possible future obsolescence of the version marking. In any case, any coredev who merges would decide about backports.

This particular issue needs major surgery as it is two interleaved but unrelated discussions. Francisco should have moved his unrelated patch to a new issue as Eric Smith asked. I opened new bpo-42299 and moved the revised patch there, with credit to Francisco and Ezio as reviewer. I unlinked both from here.

To make the discussion of Nick's issue more readable, the unrelated messages about the unrelated patch should be unlinked. I believe each message unlink would generate a separate email, as did the file unlinks.

vstinner commented 3 years ago

Since nobody implemented this idea in 8 years, maybe it's time to give up and close this issue as out of date. It seeems like Nick was busy with other stuff, and nobody took this task in the meanwhile.

abrahammurciano commented 6 months ago

Hi, I know this is old, but I believe there's still significant value in unifying these tests. I stumbled across this difference between them in python3.12 and perhaps unifying the tests would have caught it.

>>> "{[0]}".format([1])
'1'
>>> Formatter().format("{[0]}", [1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "***/python/3.12/lib/python3.12/string.py", line 190, in format
    return self.vformat(format_string, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/python/3.12/lib/python3.12/string.py", line 194, in vformat
    result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/python/3.12/lib/python3.12/string.py", line 234, in _vformat
    obj, arg_used = self.get_field(field_name, args, kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/python/3.12/lib/python3.12/string.py", line 299, in get_field
    obj = self.get_value(first, args, kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/python/3.12/lib/python3.12/string.py", line 256, in get_value
    return kwargs[key]
           ~~~~~~^^^^^
KeyError: ''

Edit: I see that this bug is already reported here: https://github.com/python/cpython/issues/71494