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

Improve string's __getitem__ error message #88276

Closed
miguendes mannequin opened this issue May 11, 2021 · 3 comments
Closed

Improve string's __getitem__ error message #88276

miguendes mannequin opened this issue May 11, 2021 · 3 comments
Labels
3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@miguendes
Copy link
Mannequin

miguendes mannequin commented May 11, 2021

BPO 44110
Nosy @serhiy-storchaka, @miguendes
PRs
  • bpo-44110: Improve string's __getitem__ error message #26042
  • 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-06-27.12:12:22.594>
    created_at = <Date 2021-05-11.20:50:25.367>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = "Improve string's __getitem__ error message"
    updated_at = <Date 2021-06-27.12:14:59.005>
    user = 'https://github.com/miguendes'

    bugs.python.org fields:

    activity = <Date 2021-06-27.12:14:59.005>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-06-27.12:12:22.594>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2021-05-11.20:50:25.367>
    creator = 'miguendes'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44110
    keywords = ['patch']
    message_count = 3.0
    messages = ['393473', '396575', '396577']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'miguendes']
    pr_nums = ['26042']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44110'
    versions = ['Python 3.11']

    @miguendes
    Copy link
    Mannequin Author

    miguendes mannequin commented May 11, 2021

    I noticed that getitem message, although helpful, could be improved a bit further. This will also make it consistent with other error messages such as the ones raised by str.count, str.split, str.endswith and so many others.

    Currently, the error message goes like this: "TypeError: string indices must be integers" but we could specify the type of the object passed as argument to __getitem__. So, for example:

    >>> idx = '1'
    >>> s = 'abcde'
    >>> s[idx]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: string indices must be integers, not 'str'
    

    This makes easier to debug and it is also consistent with other methods:

    >>> "alala".count(8)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be str, not int
    
    >>> "lala|helo".split(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be str or None, not int
    
    >>> 1 in "lala"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'in <string>' requires string as left operand, not int
    
    >>> "lala|helo".split(object())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be str or None, not object

    @miguendes miguendes mannequin added 3.10 only security fixes 3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels May 11, 2021
    @serhiy-storchaka
    Copy link
    Member

    New changeset ed10764 by Miguel Brito in branch 'main':
    bpo-44110: Improve string's __getitem__ error message (GH-26042)
    ed10764

    @serhiy-storchaka serhiy-storchaka removed the 3.10 only security fixes label Jun 27, 2021
    @serhiy-storchaka serhiy-storchaka removed the 3.10 only security fixes label Jun 27, 2021
    @serhiy-storchaka
    Copy link
    Member

    Thank you for your contribution Miguel.

    I decided to not backport this change to 3.10 because the benefit is too small in comparison with possibility to break someone's tests.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant