Skip to content

Conversation

@karlding
Copy link
Collaborator

@karlding karlding commented May 21, 2019

Here's the second part that enables black formatter checks on the project.

The black formatter returns 0 if nothing would change, and 1 if files would be reformatted, which allows the CI job to fail.

black --check .

In order to provide some more verbose error reporting, we also run the following to provide the diff and show the modifications (although this isn't strictly necessary).

black --diff .

I've split the commits for easier review so that the final commit is purely the results of running

black .

karlding added 4 commits May 20, 2019 20:58
Disable the pylint warning for the block, as black will reformat this
over multiple lines instead of the previous one-liner.
@karlding karlding requested review from felixdivo and hardbyte May 21, 2019 04:41
@karlding
Copy link
Collaborator Author

Also, I wrote this quick and dirty bash script to dump all the Python files that are currently touched by open PRs.

ACTIVE_PRS=(290 539 554 567)
DIFF_FILE="output.txt"

echo "" >| "${DIFF_FILE}"

for PR in "${ACTIVE_PRS[@]}"; do
  git fetch origin pull/"${PR}"/head:pr-"${PR}"
  git diff --name-only develop...pr-"${PR}" | grep '.py' | tee -a "${DIFF_FILE}"
done

sort "${DIFF_FILE}" -o "${DIFF_FILE}" -u

I looked at those PRs, and the changes don't seem too bad in context of this giant format patch. Otherwise, if we want to, we can simply exclude these files until those PRs are merged.

can/__init__.py
can/interface.py
can/interfaces/__init__.py
can/interfaces/pcan/pcan.py
can/interfaces/serial/__init__.py
can/interfaces/serial/serial_can.py
can/interfaces/serial/simpleserial.py
can/interfaces/serial/slcan.py
can/interfaces/slcan.py
can/interfaces/usb_can_analyzer/__init__.py
can/interfaces/usb_can_analyzer/usb_can_analyzer.py
can/io/__init__.py
can/io/mf4.py
examples/receive_all.py
examples/serial_com.py
setup.py
test/interface_simpleserial_test.py
test/interface_slcan_test.py
test/interface_test.py
test/logformats_test.py
test/serial_test.py

@codecov
Copy link

codecov bot commented May 21, 2019

Codecov Report

Merging #602 into develop will decrease coverage by 0.1%.
The diff coverage is 69.42%.

@@             Coverage Diff             @@
##           develop     #602      +/-   ##
===========================================
- Coverage    63.79%   63.68%   -0.11%     
===========================================
  Files           63       63              
  Lines         5532     5541       +9     
===========================================
  Hits          3529     3529              
- Misses        2003     2012       +9

@codecov
Copy link

codecov bot commented May 21, 2019

Codecov Report

Merging #602 into develop will decrease coverage by 0.12%.
The diff coverage is 69.29%.

@@             Coverage Diff             @@
##           develop     #602      +/-   ##
===========================================
- Coverage    63.79%   63.66%   -0.13%     
===========================================
  Files           63       63              
  Lines         5532     5543      +11     
===========================================
  Hits          3529     3529              
- Misses        2003     2014      +11

'slcan': ('can.interfaces.slcan', 'slcanBus'),
'canalystii': ('can.interfaces.canalystii', 'CANalystIIBus'),
'systec': ('can.interfaces.systec', 'UcanBus')
"kvaser": ("can.interfaces.kvaser", "KvaserBus"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want this new formatting here? Or is there no option to exclude a block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

black provides a way to exclude sections from formatting, but IMO that kind of goes against the point of having a formatter.

If someone feels strongly about keeping it untouched by the formatter, I can add

# fmt: off

# ...

# fmt: on


for channel in self.channels:
if CANalystII.VCI_InitCAN(VCI_USBCAN2, self.device, channel, byref(self.init_config)) == STATUS_ERR:
if (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
if (
status = CANalystII.VCI_InitCAN(VCI_USBCAN2, self.device, channel, byref(self.init_config))
if status == == STATUS_ERR:

timeout = -1 if timeout is None else int(timeout * 1000)

if CANalystII.VCI_Receive(VCI_USBCAN2, self.device, self.channels[0], byref(raw_message), 1, timeout) <= STATUS_ERR:
if (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split into two statements?

Copy link
Collaborator

@felixdivo felixdivo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't really like the style, but that is kind of subjective so I'm fine with it if you want it since it at least is consistent.

Maybe it would be nice to exclude some code "blocks", like in can/interfaces/__init__.py or large parts of can/interfaces/ixxat/constants.py. I don't know whether that's possible or desired.

@felixdivo
Copy link
Collaborator

I didn't go through all of the changes, since it was just tom much.

@felixdivo
Copy link
Collaborator

We could also add the badge: https://github.com/python/black#show-your-style

@felixdivo
Copy link
Collaborator

I did really like the previous semantics of quotes: Double quotes for user facing text, single quotes for everything else. But I guess that's a matter of taste as well.

@felixdivo felixdivo added the QA about improving and maintaining the quality of the library label May 21, 2019
.travis.yml Outdated
- travis_retry pip install -r requirements-lint.txt
script:
- black --diff .
- black --check .
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could image black --check --verbose . being enough, since black itself uses it

karlding added 2 commits May 23, 2019 22:10
Clean up formatting of the CANalystII interface after running Black
formatter.
Switch to using:

    black --check --verbose .

Providing the diff isn't too valuable when the formatting is
automatically handled by the formatter.
@hardbyte hardbyte added this to the 4.0 Release milestone May 27, 2019
@hardbyte hardbyte merged commit c79bba8 into hardbyte:develop May 27, 2019
@karlding karlding deleted the enable_black_formatter branch July 11, 2019 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

QA about improving and maintaining the quality of the library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants