Skip to content

Commit

Permalink
Address feedback & cleanup comments
Browse files Browse the repository at this point in the history
- I don't actually have to go on a rant about tomli and bare CR it
  turns out.
- Subclassing KeyError isn't actually *that* important for one
  custom exception.
  • Loading branch information
ichard26 committed Aug 21, 2021
1 parent e9834e0 commit f103dc0
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 24 deletions.
15 changes: 0 additions & 15 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,3 @@ follow_imports=skip
# The following is because of `patch_click()`. Remove when
# we drop Python 3.6 support.
warn_unused_ignores=False

[mypy-black.files]
# Unfortunately tomli has deprecated strings and changed the API type
# annotations to bytes all while strings still work. We still use strings
# since it's unclear whether this will last (depends on how much the
# TOML people like bare CR as a newline sequence I suppose). Anyway,
# some versions of tomli still specify str as the type so mypy complains
# that the type: ignore is unnecessary. Gosh I wish I wasn't telling
# this story.
#
# See also: https://github.com/psf/black/pull/2408
# https://github.com/pypa/pip/pull/10238 (because black is
# following whatever they do)
# https://github.com/toml-lang/toml/issues/837
warn_unused_ignores=False
2 changes: 2 additions & 0 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def validate_regex(

@click.command(
context_settings=dict(help_option_names=["-h", "--help"]),
# While Click does set this field automatically using the docstring, mypyc
# (annoyingly) strips 'em so we need to set it here too.
help="The uncompromising code formatter.",
)
@click.option("-c", "--code", type=str, help="Format the code passed in as a string.")
Expand Down
2 changes: 0 additions & 2 deletions src/black/brackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
DOT_PRIORITY: Final = 1


# Ideally this would be a subclass of KeyError, but mypyc doesn't like that.
# See also: https://mypyc.readthedocs.io/en/latest/native_classes.html#inheritance.
class BracketMatchError(Exception):
"""Raised when an opening bracket is unable to be matched to a closing bracket."""

Expand Down
2 changes: 1 addition & 1 deletion src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:
If parsing fails, will raise a tomli.TOMLDecodeError
"""
with open(path_config, encoding="utf8") as f:
pyproject_toml = tomli.load(f) # type: ignore # due to deprecated API usage
pyproject_toml = tomli.loads(f.read())
config = pyproject_toml.get("tool", {}).get("black", {})
return {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}

Expand Down
4 changes: 2 additions & 2 deletions src/black/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ class OffsetAndMagic:
magic: str


# Unsurprisingly, dataclasses are Not. An. Option. Here. Due. To. Mypyc. As. Usual.
# > fyi it's due the ast.NodeVisitor parent type
# Unsurprisingly, subclassing ast.NodeVisitor means we can't use dataclasses here
# as mypyc will generate broken code.
class MagicFinder(ast.NodeVisitor):
"""Visit cell to look for get_ipython calls.
Expand Down
7 changes: 4 additions & 3 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,10 @@ def _rhs(
line, line_length=mode.line_length, features=features
)

# HACK: functions (like rhs) compiled by mypyc don't retain their __name__
# attribute which is needed in `run_transformer` further down. Unfortunately
# a nested class breaks mypyc too. So a class must be created via type ...
# HACK: nested functions (like _rhs) compiled by mypyc don't retain their
# __name__ attribute which is needed in `run_transformer` further down.
# Unfortunately a nested class breaks mypyc too. So a class must be created
# via type ... https://github.com/mypyc/mypyc/issues/884
rhs = type("rhs", (), {"__call__": _rhs})()

if mode.experimental_string_processing:
Expand Down
5 changes: 4 additions & 1 deletion src/black/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ def normalize_string_prefix(s: str, remove_u_prefix: bool = False) -> str:
return f"{new_prefix}{match.group(2)}"


@lru_cache(maxsize=256)
# Re(gex) does actually cache patterns internally but this still improves
# performance on a long list literal of strings by 5-9% since lru_cache's
# caching overhead is much lower.
@lru_cache(maxsize=64)
def _cached_compile(pattern: str) -> re.Pattern:
return re.compile(pattern)

Expand Down

0 comments on commit f103dc0

Please sign in to comment.