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

A better assert statement #70889

Open
warsaw opened this issue Apr 6, 2016 · 6 comments
Open

A better assert statement #70889

warsaw opened this issue Apr 6, 2016 · 6 comments
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@warsaw
Copy link
Member

warsaw commented Apr 6, 2016

BPO 26702
Nosy @warsaw, @rhettinger, @serhiy-storchaka, @ammaraskar, @iritkatriel

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 2016-04-06.14:43:37.026>
labels = ['type-feature', '3.10']
title = 'A better assert statement'
updated_at = <Date 2021-03-29.03:22:25.960>
user = 'https://github.com/warsaw'

bugs.python.org fields:

activity = <Date 2021-03-29.03:22:25.960>
actor = 'ammar2'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2016-04-06.14:43:37.026>
creator = 'barry'
dependencies = []
files = []
hgrepos = []
issue_num = 26702
keywords = []
message_count = 6.0
messages = ['262946', '262948', '262949', '264940', '389653', '389655']
nosy_count = 5.0
nosy_names = ['barry', 'rhettinger', 'serhiy.storchaka', 'ammar2', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue26702'
versions = ['Python 3.10']

@warsaw
Copy link
Member Author

warsaw commented Apr 6, 2016

Too many times I hit failing assert statements, and have no idea what value is causing the assertion to fail. Sure, you can provide a value to print (instead of just the failing code) but it seems to be fairly rarely used. And it can also lead to code duplication. As an example, I saw this today in some installed code:

assert k.replace('.', '').replace('-', '').replace('_', '').isalum()

So now I have to sudo edit the installed system file, duplicate everything up to but not including the .isalum() as the second argument to assert, then try to reproduce the problem. IWBNI assert could make this better.

One idea would be to split the value and the conditional being asserted on that value, but we can't use two-argument asserts for that. Crazy syntax thought: reuse the 'with' keyword:

assert k.replace('.', '').replace('-', '').replace('_', '') with isalum

where the part before the 'with' is 'value' and the part after the 'with' is conditional, and the two parts together imply the expression.

This would be equivalent to:

if __debug__:
    if not value.conditional():
        raise AssertionError(expression, value, conditional)

I suppose you then want to support arguments:

assert foo with can_bar, 1, 2, x=3

but maybe that's YAGNI and we can just say to use a better 2-value assert in those more complicated cases.

@serhiy-storchaka
Copy link
Member

I think in this particular case you are more interesting in the value of k than k.replace('.', '').replace('-', '').replace('_', '').

@warsaw
Copy link
Member Author

warsaw commented Apr 6, 2016

On Apr 06, 2016, at 03:07 PM, Serhiy Storchaka wrote:

I think in this particular case you are more interesting in the value of k
than k.replace('.', '').replace('-', '').replace('_', '').

Possibly so.

@serhiy-storchaka
Copy link
Member

I'm -1 for changing syntax of assert.

But I think that it may be useful to change semantic of assert so that all intermediate results of subexpressions are saved and included in the assert report (or at least their shortened reprs). I'm +0 for this.

@iritkatriel
Copy link
Member

Maybe we could use the walrus operator for this: make assert display the reprs of all named sub-expressions.

@iritkatriel iritkatriel added 3.10 only security fixes type-feature A feature request or enhancement labels Mar 28, 2021
@ammaraskar
Copy link
Member

Just a note and possible design inspiration, pytest has pretty assertions like this to allowing you to write unit tests purely with the assert statement and not unittest's special assert... methods: https://docs.pytest.org/en/stable/example/reportingdemo.html#tbreportdemo

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

4 participants