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

Uncaught AttributeError in unittest.mock._get_target #85575

Closed
gothill mannequin opened this issue Jul 26, 2020 · 6 comments
Closed

Uncaught AttributeError in unittest.mock._get_target #85575

gothill mannequin opened this issue Jul 26, 2020 · 6 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@gothill
Copy link
Mannequin

gothill mannequin commented Jul 26, 2020

BPO 41403
Nosy @cjw296, @voidspace, @mariocj89, @tirkarthi, @iritkatriel, @gothill, @akulakov
PRs
  • bpo-41403: Improve error message for invalid mock target #30833
  • [3.10] bpo-41403: Improve error message for invalid mock target (GH-30833) #30834
  • [3.9] bpo-41403: Improve error message for invalid mock target (GH-30833) #30835
  • 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 2022-01-23.19:35:34.327>
    created_at = <Date 2020-07-26.19:15:44.076>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'Uncaught AttributeError in unittest.mock._get_target'
    updated_at = <Date 2022-01-23.19:35:34.327>
    user = 'https://github.com/gothill'

    bugs.python.org fields:

    activity = <Date 2022-01-23.19:35:34.327>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-23.19:35:34.327>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2020-07-26.19:15:44.076>
    creator = 'webisteme'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41403
    keywords = ['patch']
    message_count = 6.0
    messages = ['374339', '374362', '409472', '411400', '411410', '411411']
    nosy_count = 7.0
    nosy_names = ['cjw296', 'michael.foord', 'mariocj89', 'xtreak', 'iritkatriel', 'webisteme', 'andrei.avk']
    pr_nums = ['30833', '30834', '30835']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41403'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @gothill
    Copy link
    Mannequin Author

    gothill mannequin commented Jul 26, 2020

    When calling mock.patch incorrectly, as in the following example, an uncaught error is thrown:

    >>> from unittest import mock
    >>> class Foo:
    ...     pass
    ... 
    >>> mock.patch(Foo())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1624, in patch
        getter, attribute = _get_target(target)
      File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1469, in _get_target
        target, attribute = target.rsplit('.', 1)
    AttributeError: 'Foo' object has no attribute 'rsplit'

    This can happen when confusing mock.patch with mock.patch.object. However, the uncaught error is not informative, as it does not indicate that the wrong type of object was passed to mock.patch.

    @gothill gothill mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 26, 2020
    @cjw296
    Copy link
    Contributor

    cjw296 commented Jul 27, 2020

    Given that mock.patch is being used incorrectly here, the error message seems clear enough: It's saying there's a Foo object in place, and rsplit gives a strong indication that a string was expected.

    Would adding type hints in mock.py be a resolution for this?

    I'd be fine to see this bug closed with "wontfix", but will leave it open for now in case others feel strongly differently.

    @akulakov
    Copy link
    Contributor

    akulakov commented Jan 1, 2022

    More generally, split()/rsplit() are probably the most common operations done on expected string at the start of a function, i.e. most likely to be triggered on the wrong type passed in. At the same time to many new users split()/rsplit() does not imply anything related to strings, and 'split' sounds too generic to expect explanatory results from google search. (although the first few results do point to the right explanation, and rsplit does seem like something that would return helpful search results).

    I wonder if it would be helpful, especially for .split() (and rsplit for consistency), to add to the error msg: "... it's likely that a string object was expected instead of <type> object".

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes and removed 3.7 (EOL) end of life labels Jan 14, 2022
    @iritkatriel
    Copy link
    Member

    New changeset f7955a8 by Irit Katriel in branch 'main':
    bpo-41403: Improve error message for invalid mock target (GH-30833)
    f7955a8

    @iritkatriel
    Copy link
    Member

    New changeset e3ade66 by Irit Katriel in branch '3.10':
    bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30834)
    e3ade66

    @iritkatriel
    Copy link
    Member

    New changeset 1398dca by Irit Katriel in branch '3.9':
    bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30835)
    1398dca

    @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.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants