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

Add mock_import method to mock module #71563

Closed
EyalPosener mannequin opened this issue Jun 23, 2016 · 7 comments
Closed

Add mock_import method to mock module #71563

EyalPosener mannequin opened this issue Jun 23, 2016 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@EyalPosener
Copy link
Mannequin

EyalPosener mannequin commented Jun 23, 2016

BPO 27376
Nosy @voidspace, @iritkatriel
Files
  • mock_import.patch: a patch with the new method
  • 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 = 'https://github.com/voidspace'
    closed_at = <Date 2022-04-02.18:20:53.303>
    created_at = <Date 2016-06-23.17:12:17.299>
    labels = ['type-feature', 'library']
    title = 'Add mock_import method to mock module'
    updated_at = <Date 2022-04-02.18:20:53.302>
    user = 'https://bugs.python.org/EyalPosener'

    bugs.python.org fields:

    activity = <Date 2022-04-02.18:20:53.302>
    actor = 'iritkatriel'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2022-04-02.18:20:53.303>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2016-06-23.17:12:17.299>
    creator = 'Eyal Posener'
    dependencies = []
    files = ['43522']
    hgrepos = []
    issue_num = 27376
    keywords = ['patch']
    message_count = 7.0
    messages = ['269127', '274103', '274104', '274106', '274108', '274153', '416574']
    nosy_count = 4.0
    nosy_names = ['michael.foord', 'fruch', 'Eyal Posener', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27376'
    versions = ['Python 3.6']

    @EyalPosener
    Copy link
    Mannequin Author

    EyalPosener mannequin commented Jun 23, 2016

    Add mock_import method.

    A helper function to mask ImportErrors on a scoped code, using the with statement, or in method a method used as a decorator.
    Failed imports will be ignored, unless specified by the do_not_mock argument.

    The do_not_mock argument is a package or module name, or package or module names list. When specified, and imported in the scoped mocked code, importing them must succeed. If None (the default) then no import must succeed.

    @EyalPosener EyalPosener mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 23, 2016
    @voidspace voidspace self-assigned this Sep 1, 2016
    @voidspace
    Copy link
    Contributor

    Is this for mocking out runtime dependencies that aren't available at test time? It seems like a good way of masking bugs! I'd be happier with a (or at least an option) to specify the imports that should be mocked. The use case should be mentioned in the docs.

    I think the name is slightly confusing. I originally thought this was a function to mock specific imports - not to catch failed imports. mock_missing_import (or similar) would be a better name.

    It's common with the mock functions to be able to provide a class to use as the mock function, and to take arbitrary keyword arguments to pass to the mock constructor.

    @EyalPosener
    Copy link
    Mannequin Author

    EyalPosener mannequin commented Sep 1, 2016

    Thanks for the review, Michael.

    About the use case: I use it for a process with loads code and inspect it's classes and methods. When I run this process, not always I have all the dependencies of the inspected code, so I found myself mocking all those packages before running the inspection code. This was very inconvenience, and broke any time someone added a new dependency to the code which is not in the standard library.

    About the name: I agree.

    About the keyword for the mock constructor: no problems.

    Should I fix the code and submit an updated patch?
    Do you think this function has a place in the standard mock module?

    @voidspace
    Copy link
    Contributor

    It's not a use case I've specifically had but I can see its use. I'm uncertain of whether that means it belongs in the module or not. Let me see if I can get some more eyes on this bug.

    @voidspace
    Copy link
    Contributor

    Before you spend any more time on this, my current thinking is that this is a bit too specialised to belong in the standard library. I'll wait and see if a preponderance of core devs and other users disagree with me before I close this though.

    @fruch
    Copy link
    Mannequin

    fruch mannequin commented Sep 1, 2016

    Some real use cases is needed, like testing a code that behave differently on case of package availability.

    I think something like patch for modules can be useful here, so you could have some control on what would be returned.

    @iritkatriel
    Copy link
    Member

    Closing as that's where the discussion of 2016 was leaning. Please raise this on python-ideas if you would like to bring it up again.

    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants