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

Avoid looking at __name__ attribute in black.linegen.run_transformer #2432

Open
ichard26 opened this issue Aug 18, 2021 · 0 comments
Open

Avoid looking at __name__ attribute in black.linegen.run_transformer #2432

ichard26 opened this issue Aug 18, 2021 · 0 comments
Labels
C: cleanup Refactoring and removing dust :) C: maintenance Related to project maintenance, e.g. CI, testing, policy changes, releases

Comments

@ichard26
Copy link
Collaborator

Right the transformer code looks at the transformer's __name__ attribute when deciding to return early:

black/src/black/linegen.py

Lines 964 to 967 in ef7c45f

if (
transform.__name__ != "rhs"
or not line.bracket_tracker.invisible
or any(bracket.value for bracket in line.bracket_tracker.invisible)

This is kinda hacky and becomes quite a problem when integrating mypyc since nested functions don't keep that attribute due a bug (mypyc/mypyc#884). The current workaround is to dynamtically create a wrapping class at runtime (via type() mind you 🙃) which do keep their __name__ attribute. Let's avoid this entire situation if feasible ^^

Additional context:

I guess it would be a bigger refactor but I'd like it better if we just stopped using __name__.

by @JelleZijlstra in #2431 (comment)

That's a bit hacky; maybe we should turn the Transformer type into a pair instead to store this extra information.

by @JelleZijlstra in #1621 (comment)

@ichard26 ichard26 added C: cleanup Refactoring and removing dust :) C: maintenance Related to project maintenance, e.g. CI, testing, policy changes, releases labels Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: cleanup Refactoring and removing dust :) C: maintenance Related to project maintenance, e.g. CI, testing, policy changes, releases
Projects
None yet
Development

No branches or pull requests

1 participant