peterjc / flake8-black

flake8 plugin to run black for checking Python coding style
MIT License
165 stars 10 forks source link

Unexpected exception: int object expected; got str #59

Closed eggplants closed 2 years ago

eggplants commented 2 years ago

ref: #47, https://github.com/RDFLib/rdflib/discussions/1927#discussioncomment-2750968

when I use flake8-black in rdflib, rdflib/***.py:0:1: BLK999 Unexpected exception: int object expected; got str is raised 89 times.

$ flake8 --version
4.0.1 (black: 0.3.2, mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0) CPython 3.9.10 on Linux
$ git clone https://github.com/RDFLib/rdflib && cd rdflib
...
$ flake8 rdflib | grep "BLK999 Unexpected exception" | wc -l
89
peterjc commented 2 years ago

Ah yes, you raised this on #47 but we never managed to reproduce this.

Can you confirm which version of black you have installed?

eggplants commented 2 years ago
$ black --version
black, 22.3.0 (compiled: yes)

This problem confirmed to happen in not only my environment but also other people's ones...

peterjc commented 2 years ago

Good news - I can reproduce that on macOS with Python 3.8, so not OS nor Python version specific:

$ flake8 --version
4.0.1 (black: 0.3.2, mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0) CPython 3.8.5 on Darwin

Disabling my error catching,

...
  File "/Users/xxx/repositories/flake8-black/flake8_black.py", line 212, in run
    file_mode = self._file_mode
  File "/Users/xxx/repositories/flake8-black/flake8_black.py", line 123, in _file_mode
    black_config[path] = load_black_mode(path)
  File "/Users/xxx/repositories/flake8-black/flake8_black.py", line 69, in load_black_mode
    return black.FileMode(
  File "<string>", line 4, in __init__
TypeError: int object expected; got str

I'll get back to this later - but am optimistic about fixing this now. Thanks!

peterjc commented 2 years ago

Also breaks with black 22.1.0, and I have a work around for you:

$ git diff
diff --git a/pyproject.toml b/pyproject.toml
index efe383b0..a672bdd7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.black]
 required-version = "22.3.0"
-line-length = "88"
+line-length = 88
 skip-string-normalization = true
 target-version = ['py37']
 include = '\.pyi?$'

It looks like your TOML has line-length defined as a string, which I am passing to black as is when black wants and integer.

peterjc commented 2 years ago

Release v0.3.3 should fix this. Thank you for the reproducible example, incredibly helpful.