Skip to content

Coding style suggestion about nesting constructs #1184

Closed

Description

@gvanrossum

There are a lot of different patterns in the mypy codebase where some stack is maintained by a pair of functions that push something onto the stack (or bump a counter or something) and pop it off again. So you'll see code like this:

            self.msg.disable_errors()
            arg_types = self.infer_arg_types_in_context(None, args)
            self.msg.enable_errors()

I wonder if this pattern couldn't be made clearer in the code by using context managers. In this case, we would write e.g.

            with self.msg.disable_errors():
                arg_types = self.infer_arg_types_in_context(None, args)

The implementation would be very simple:

@contextmanager
def disable_errors(self) -> None:
    self.disable_count += 1
    yield
    self.disable_count -= 1

(I'm picking on this example because it came up recently, but there are a lot of function pairs like this; e.g. check_func_item() (https://github.com/JukkaL/mypy/blob/master/mypy/checker.py#L542) has four different stacks it manipulates.)

Activity

  1. JukkaL commented on Jan 28, 2016

    @JukkaL
    Collaborator

    Yeah, makes sense. The original code was written before there was support for @contextmanager.

  2. added this to the milestone on Mar 1, 2016
  3. removed this from the milestone on Mar 29, 2017
  4. kishdubey commented on Nov 4, 2020

    @kishdubey

    Hello, would like to take this one, if it is still available. This would be my first issue tackled in the project. The approach will be to edit the disable_errors() method in messages.py, as shown above. Then change each occurrence of the disable_error() and enable_error() pair to what is shown above.

    I have a question, what to do when these functions show up alone, as in not in pairs?

    e.g, at https://github.com/python/mypy/blob/master/mypy/checkexpr.py#L862 or https://github.com/python/mypy/blob/master/mypy/checkexpr.py#L2803

    In addition, what other function pairs are there that have this same pattern?

  5. hauntsaninja commented on Nov 4, 2020

    @hauntsaninja
    Collaborator

    Thanks for volunteering!

    In both the examples you link, it looks to me like it appears in pairs, although the second one is tricky and you'd need something like contextlib.nullcontext to get it to work.
    But if there are calls that are hard to convert, it's fine to leave them alone.

    I'm not sure what other function calls occur in pairs. It's a good excuse to browse and get to know the mypy source :-)

  6. dixith commented on Mar 25, 2021

    @dixith
    Contributor

    @kishdubey Are you still working on this issue? Can i take over if u are not working on it?

  7. JukkaL commented on Mar 26, 2021

    @JukkaL
    Collaborator

    @dixith Since there hasn't been an update in several months, it's okay for you to work on this if there's no response.

  8. added a commit that references this issue on Sep 6, 2021
  9. JukkaL commented on Jun 15, 2022

    @JukkaL
    Collaborator

    A bunch of these have been switched to use context managers. We can close this now. If there are remaining cases that should be fixed, we can create a follow-up issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions