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

Coding style suggestion about nesting constructs #1184

Closed
gvanrossum opened this issue Jan 28, 2016 · 6 comments
Closed

Coding style suggestion about nesting constructs #1184

gvanrossum opened this issue Jan 28, 2016 · 6 comments

Comments

@gvanrossum
Copy link
Member

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

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 28, 2016

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

@JukkaL JukkaL added the refactoring Changing mypy's internals label Jan 28, 2016
@ddfisher ddfisher added this to the 0.4.0 milestone Mar 1, 2016
@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@kishdubey
Copy link

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?

@hauntsaninja
Copy link
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 :-)

@dixith
Copy link
Contributor

dixith commented Mar 25, 2021

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

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 26, 2021

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

JukkaL pushed a commit that referenced this issue Sep 6, 2021
Related issue: #1184
Follows up to #10569, #10685

This PR:
* Refactors `Scope`
  - Replaces `Scope.enter_file` with `Scope.module_scope`
  - Replaces `Scope.enter_function` with `Scope.function_scope`
  - Splits `Scope.leave()` into corresponding context manager
* Deletes unused files
JukkaL pushed a commit that referenced this issue Sep 22, 2021
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 15, 2022

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.

@JukkaL JukkaL closed this as completed Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants