# Mismatch between assert's semantics and how it's used (-O, -OO, disable)

**URL:** <https://discuss.python.org/t/mismatch-between-asserts-semantics-and-how-its-used-o-oo-disable/29282>\
**Category:** Ideas\
**Created:** [July 8, 2023, 9:14pm UTC](https://discuss.python.org/t/mismatch-between-asserts-semantics-and-how-its-used-o-oo-disable/29282 "2023-07-08T21:14:48Z")\
**Posts on this page:** 1\
**Showing post:** 49

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [July 13, 2023, 2:39pm UTC](https://discuss.python.org/t/mismatch-between-asserts-semantics-and-how-its-used-o-oo-disable/29282/49 "2023-07-13T14:39:38Z")

</div>

> [@pf\_moore](#):
>
> IMO, that’s a very optimistic assumption to make - I’d assume they detect `-O` and so do the modifications if it’s present, so it would _not_ “continue to work”.

I will admit to being imprecise with my wording. If a test framework relies on assertions and doesn’t change in the 2-3 year deprecation period, it would stop working. But [if I understand this correctly](https://discuss.python.org/t/stop-ignoring-asserts-when-running-in-optimized-mode/13132/20), `pytest` replaces the assert statements in test files already without checking these flags.

> [@pf\_moore](#):
>
> If you can find a way of flagging the issue with `-O` to people who currently write code that relies on asserts not being stripped, without affecting working code, I’d listen.

😕 I thought I was suggesting such a thing. In this scenario the issue isn’t with `-O`, it’s with the standard runtime mode. Users of `-O` will see no change in behavior.

If code is using assertions as-documented (used as invariants, should never raise unless there are bad assumptions), nothing should change and no noise will be created. The relevant changes to them are a) testing frameworks that rely on `assert` need to be aware of this and b) in any other case where they need `assert` evaluated, they should use `python -X dev` rather than `python [a.k.a. debug]`, which makes the use case more explicit.

If some code is using them _incorrectly_ (it raises assertion errors as a part of normal operation, as a way to check input or something), there will be warnings that these uses need to be changed into a proper check that always runs. This tells people who are doing the wrong thing to change their code and make it correct. I’m not trying to “let them off” from writing buggy code. I’m warning exactly those people, and telling them how to fix it. What am I misunderstanding here?

---

_[View the full topic](https://discuss.python.org/t/mismatch-between-asserts-semantics-and-how-its-used-o-oo-disable/29282)._
