Skip to content

Coding style suggestion about nesting constructs #1184

Closed
@gvanrossum

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.)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions