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

add new line after reading requirements file #415

Merged
merged 3 commits into from
Sep 28, 2019
Merged

add new line after reading requirements file #415

merged 3 commits into from
Sep 28, 2019

Conversation

barakreif
Copy link
Contributor

@barakreif barakreif commented Sep 28, 2019

Resolves #414

  • add new line post reading requirements file.
  • change before variable from tuple .
  • to list, add test cases.

This maintains all previous behaviour(s) as given by the test cases.

(my first ever OS contribution, any and all feedback is welcome)

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

this looks awesome -- thanks for fixing this! Just one little thing (which'll adjust the tests slightly)

congrats on your first open source contribution \o/

@@ -94,7 +99,7 @@ def fix_requirements(f): # type: (IO[bytes]) -> int

after_string = b''.join(after)

if before_string == after_string:
if before_string.rstrip() == after_string.rstrip():
Copy link
Member

Choose a reason for hiding this comment

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

I would just leave this line alone personally -- I think adding a newline to the end of the file is a good fix in the general case 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do, but as a code design question, why not to run the end_of_file_fixer at first as is?

Copy link
Member

Choose a reason for hiding this comment

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

Some prefer to pick and choose hooks, I agree someone should probably be configuring the end-of-file-fixer as one of their default hooks -- but it's probably ok to have the behaviour happen here as well

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

looks good!

@@ -40,11 +40,16 @@ def __lt__(self, requirement): # type: (Requirement) -> int

def fix_requirements(f): # type: (IO[bytes]) -> int
requirements = [] # type: List[Requirement]
before = tuple(f)
before = list(f) # type: List[bytes]
Copy link
Member

Choose a reason for hiding this comment

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

just an aside, the type annotation isn't required here -- mypy will properly infer that this is a list of bytes objects so generally you can leave it out -- I usually prefer to only type annotate things when mypy requires it (the inference often saves some keyboard-typing and importing)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am always worried from this, in case 'read' will return a single stream if its overloaded somehow:

>>> a=b'12'
>>> list(a)
[49, 50]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have seen the above happen when 2to3 conversion was mishandled for bytestreams.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, fortunately here the __iter__ function from IO[bytes] will give bytes (and mypy knows this)

@asottile asottile merged commit 8884b63 into pre-commit:master Sep 28, 2019
@asottile
Copy link
Member

One tiny thing, I edited your description to say "Resolves #..." -- that way when the PR is merged it automatically closes the issue that it is referencing

Thanks again for the patch! It's greatly appreciated :)

@asottile
Copy link
Member

this has been released in v2.4.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

requirements-txt-fixer corrupts file if no new line at end
2 participants