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

SyntaxWarning for "assertion is always true, perhaps remove parentheses?" does not work with constants #87663

Closed
tsukasa-au mannequin opened this issue Mar 15, 2021 · 5 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@tsukasa-au
Copy link
Mannequin

tsukasa-au mannequin commented Mar 15, 2021

BPO 43497
Nosy @markshannon, @serhiy-storchaka, @tsukasa-au
PRs
  • bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. #24867
  • 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 = <Date 2021-07-28.22:58:30.385>
    created_at = <Date 2021-03-15.07:31:24.963>
    labels = ['interpreter-core', 'type-bug', '3.8', '3.9', '3.10', '3.7']
    title = 'SyntaxWarning for "assertion is always true, perhaps remove parentheses?" does not work with constants'
    updated_at = <Date 2021-07-28.22:58:30.385>
    user = 'https://github.com/tsukasa-au'

    bugs.python.org fields:

    activity = <Date 2021-07-28.22:58:30.385>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-28.22:58:30.385>
    closer = 'iritkatriel'
    components = ['Interpreter Core']
    creation = <Date 2021-03-15.07:31:24.963>
    creator = 'darke'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43497
    keywords = ['patch']
    message_count = 5.0
    messages = ['388711', '388716', '388718', '388723', '388835']
    nosy_count = 3.0
    nosy_names = ['Mark.Shannon', 'serhiy.storchaka', 'darke']
    pr_nums = ['24867']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue43497'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    @tsukasa-au
    Copy link
    Mannequin Author

    tsukasa-au mannequin commented Mar 15, 2021

    The following block of code does not produce a SyntaxWarning in python 3.7 and above (it does produce a warning in python 3.6 and below):

    assert(False, 'msg')
    

    If the tuple is not a constant (for example (x, 'msg')), then a warning is still produced.

    @tsukasa-au tsukasa-au mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 15, 2021
    @serhiy-storchaka
    Copy link
    Member

    What is the use case for assert with a constant?

    @tsukasa-au
    Copy link
    Mannequin Author

    tsukasa-au mannequin commented Mar 15, 2021

    I would argue that there is none (especially if it is tuple/something that is always true) -- thus why I would assume that Python would provide a warning.

    This bug comes from a discussion I was having with someone earlier today where they mentioned that it would be nice if the linter complained about the following::

    assert 'not reachable'

    That code is incorrect (the assertion will never fire, due to the string evaluating to True). We remembered that python did complain about tuples, and tried to see what the warning looked like using a trivial example::

      assert(False, 'msg')

    We noticed that this code did not produce a warning on python3.8, but did produce a warning on python2.7.

    After much digging we found that the following did work on python3.8::

      assert(False, str())

    This allowed us to deduce that something special was happening with constants (well, it also required us to look at the disassembly of the ops codes for the above two statements).

    Going back to your original question, I have used assert False in code to "test" if a piece of code was being reached. I have also used it for things similar to the piece of code that started all of this::

      if some_expression:
        # ...
      elif some_other_expression:
        #...
      else:
        assert False, 'This code is unreachable'

    @serhiy-storchaka
    Copy link
    Member

    In such case I prefer to write

    assert not 'reachable'
    

    It is shorter than writing False.

    If I want to keep an exception even with -O, I write

        raise AssertionError

    @markshannon
    Copy link
    Member

    New changeset a8ef457 by tsukasa-au in branch 'master':
    bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. (GH-24867)
    a8ef457

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 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

    3 participants