Added Python and grammar versions to a parser error message (#4378)
--------- Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Co-authored-by: Shantanu Jain <hauntsaninja@gmail.com>
This commit is contained in:
parent
f50aba4984
commit
98a580bbdc
@ -47,6 +47,8 @@
|
|||||||
|
|
||||||
<!-- Changes to Black's terminal output and error messages -->
|
<!-- Changes to Black's terminal output and error messages -->
|
||||||
|
|
||||||
|
- Added Python target version information on parse error (#4378)
|
||||||
|
|
||||||
### _Blackd_
|
### _Blackd_
|
||||||
|
|
||||||
<!-- Changes to blackd -->
|
<!-- Changes to blackd -->
|
||||||
|
@ -26,6 +26,10 @@ class TargetVersion(Enum):
|
|||||||
PY312 = 12
|
PY312 = 12
|
||||||
PY313 = 13
|
PY313 = 13
|
||||||
|
|
||||||
|
def pretty(self) -> str:
|
||||||
|
assert self.name[:2] == "PY"
|
||||||
|
return f"Python {self.name[2]}.{self.name[3:]}"
|
||||||
|
|
||||||
|
|
||||||
class Feature(Enum):
|
class Feature(Enum):
|
||||||
F_STRINGS = 2
|
F_STRINGS = 2
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
import ast
|
import ast
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
import warnings
|
||||||
from typing import Iterable, Iterator, List, Set, Tuple
|
from typing import Collection, Iterator, List, Set, Tuple
|
||||||
|
|
||||||
from black.mode import VERSION_TO_FEATURES, Feature, TargetVersion, supports_feature
|
from black.mode import VERSION_TO_FEATURES, Feature, TargetVersion, supports_feature
|
||||||
from black.nodes import syms
|
from black.nodes import syms
|
||||||
@ -52,12 +52,20 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
|
|||||||
return grammars
|
return grammars
|
||||||
|
|
||||||
|
|
||||||
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
|
def lib2to3_parse(
|
||||||
|
src_txt: str, target_versions: Collection[TargetVersion] = ()
|
||||||
|
) -> Node:
|
||||||
"""Given a string with source, return the lib2to3 Node."""
|
"""Given a string with source, return the lib2to3 Node."""
|
||||||
if not src_txt.endswith("\n"):
|
if not src_txt.endswith("\n"):
|
||||||
src_txt += "\n"
|
src_txt += "\n"
|
||||||
|
|
||||||
grammars = get_grammars(set(target_versions))
|
grammars = get_grammars(set(target_versions))
|
||||||
|
if target_versions:
|
||||||
|
max_tv = max(target_versions, key=lambda tv: tv.value)
|
||||||
|
tv_str = f" for target version {max_tv.pretty()}"
|
||||||
|
else:
|
||||||
|
tv_str = ""
|
||||||
|
|
||||||
errors = {}
|
errors = {}
|
||||||
for grammar in grammars:
|
for grammar in grammars:
|
||||||
drv = driver.Driver(grammar)
|
drv = driver.Driver(grammar)
|
||||||
@ -73,14 +81,14 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
faulty_line = "<line number missing in source>"
|
faulty_line = "<line number missing in source>"
|
||||||
errors[grammar.version] = InvalidInput(
|
errors[grammar.version] = InvalidInput(
|
||||||
f"Cannot parse: {lineno}:{column}: {faulty_line}"
|
f"Cannot parse{tv_str}: {lineno}:{column}: {faulty_line}"
|
||||||
)
|
)
|
||||||
|
|
||||||
except TokenError as te:
|
except TokenError as te:
|
||||||
# In edge cases these are raised; and typically don't have a "faulty_line".
|
# In edge cases these are raised; and typically don't have a "faulty_line".
|
||||||
lineno, column = te.args[1]
|
lineno, column = te.args[1]
|
||||||
errors[grammar.version] = InvalidInput(
|
errors[grammar.version] = InvalidInput(
|
||||||
f"Cannot parse: {lineno}:{column}: {te.args[0]}"
|
f"Cannot parse{tv_str}: {lineno}:{column}: {te.args[0]}"
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -87,4 +87,6 @@ def test_patma_invalid() -> None:
|
|||||||
with pytest.raises(black.parsing.InvalidInput) as exc_info:
|
with pytest.raises(black.parsing.InvalidInput) as exc_info:
|
||||||
assert_format(source, expected, mode, minimum_version=(3, 10))
|
assert_format(source, expected, mode, minimum_version=(3, 10))
|
||||||
|
|
||||||
exc_info.match("Cannot parse: 10:11")
|
exc_info.match(
|
||||||
|
"Cannot parse for target version Python 3.10: 10:11: case a := b:"
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user