-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
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
base: master
Are you sure you want to change the base?
Conversation
for number in range(100): | ||
# It just don't do anything but for loop is still valid. | ||
pass | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_pass_in_loop
refactored with the following changes:
- Remove nested block which has no effect (
remove-empty-nested-block
)
This removes the following comments ( why? ):
# It just don't do anything but for loop is still valid.
return 'Hello ' + self.name | ||
return f'Hello {self.name}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_class_definition.GreetingClass.say_hello
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
return 'Goodbye ' + self.name | ||
return f'Goodbye {self.name}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_class_definition.GreetingClass.say_goodbye
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
return self.get_name() + ', ' + self.staff_id | ||
return f'{self.get_name()}, {self.staff_id}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Employee.get_full_id
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
for number in range(0, 10): | ||
for number in range(10): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_continue_statement
refactored with the following changes:
- Replace range(0, x) with range(x) (
remove-zero-from-range
)
|
||
# 'Py' 'thon' | ||
python = 'Py' 'thon' | ||
assert python == 'Python' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_string_operators
refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass
) - Inline variable that is only used once (
inline-variable
) - Remove
assert True
statements (remove-assert-true
) - Simplify x == x -> True and x != x -> False (
equality-identity
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
This removes the following comments ( why? ):
# 'Py' 'thon'
# To use formatted string literals, begin a string with f or F before the opening quotation | ||
# mark or triple quotation mark. Inside this string, you can write a Python expression | ||
# between { and } characters that can refer to variables or literal values. | ||
year = 2018 | ||
event = 'conference' | ||
|
||
assert f'Results of the {year} {event}' == 'Results of the 2018 conference' | ||
assert f'Results of the 2018 {event}' == 'Results of the 2018 conference' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_string_formatting
refactored with the following changes:
- Inline variable that is only used once (
inline-variable
) - Remove redundant pass statement (
remove-redundant-pass
) - Move assignment closer to its usage within a block (
move-assign-in-block
) - Simplify unnecessary nesting, casting and constant values in f-strings (
simplify-fstring-formatting
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
) - Use str.join() instead of for loop (
use-join
) - Remove
assert True
statements (remove-assert-true
) - Replace call to format with f-string. (
use-fstring-for-formatting
) - Simplify x == x -> True and x != x -> False (
equality-identity
) - Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
This removes the following comments ( why? ):
# To use formatted string literals, begin a string with f or F before the opening quotation
# The String format() Method
# between { and } characters that can refer to variables or literal values.
# Basic usage of the str.format() method looks like this:
# mark or triple quotation mark. Inside this string, you can write a Python expression
fruits_tuple_via_constructor = tuple(("apple", "banana", "cherry")) | ||
fruits_tuple_via_constructor = "apple", "banana", "cherry" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_tuples
refactored with the following changes:
- Unwrap a constant iterable constructor. (
unwrap-iterable-construction
) - Simplify sequence length comparison (
simplify-len-comparison
)
@@ -23,7 +23,6 @@ | |||
def test_type_casting_to_integer(): | |||
"""Type casting to integer""" | |||
|
|||
assert int(1) == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_type_casting_to_integer
refactored with the following changes:
- Remove
assert True
statements (remove-assert-true
) - Remove redundant pass statement (
remove-redundant-pass
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
) - Simplify x == x -> True and x != x -> False (
equality-identity
)
assert float(2.8) == 2.8 | ||
assert 2.8 == 2.8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_type_casting_to_float
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 1.01%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
assert float("3") == 3.0 | ||
assert float("4.2") == 4.2 | ||
|
||
|
||
def test_type_casting_to_string(): | ||
"""Type casting to string""" | ||
|
||
assert str("s1") == 's1' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_type_casting_to_string
refactored with the following changes:
- Remove
assert True
statements (remove-assert-true
) - Remove redundant pass statement (
remove-redundant-pass
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
) - Simplify x == x -> True and x != x -> False (
equality-identity
)
except NameError: | ||
except (NameError, ZeroDivisionError): | ||
# We should get here because of division by zero. | ||
exception_has_been_handled = True | ||
except ZeroDivisionError: | ||
# We should get here because of division by zero. | ||
exception_has_been_handled = True | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_handle_exceptions
refactored with the following changes:
- Merge except handlers with identical code blocks (
merge-except-handler
)
binary_file = open('src/files/binary_file', 'r') | ||
|
||
# To read a file’s contents, call f.read(size), which reads some quantity of data and returns | ||
# it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric | ||
# argument. When size is omitted or negative, the entire contents of the file will be read and | ||
# returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, | ||
# at most size bytes are read and returned. If the end of the file has been reached, f.read() | ||
# will return an empty string (''). | ||
read_data = multi_line_file.read() | ||
|
||
# pylint: disable=duplicate-code | ||
assert read_data == 'first line\nsecond line\nthird line' | ||
|
||
# To change the file object’s position, use f.seek(offset, from_what). The position is computed | ||
# from adding offset to a reference point; the reference point is selected by the from_what | ||
# argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current | ||
# file position, and 2 uses the end of the file as the reference point. from_what can be omitted | ||
# and defaults to 0, using the beginning of the file as the reference point. | ||
assert binary_file.seek(0) == 0 # Go to the 0th byte in the file | ||
assert binary_file.seek(6) == 6 # Go to the 6th byte in the file | ||
|
||
assert binary_file.read(1) == '6' | ||
|
||
# f.readline() reads a single line from the file; a newline character (\n) is left at the end | ||
# of the string, and is only omitted on the last line of the file if the file doesn’t end in a | ||
# newline. This makes the return value unambiguous; if f.readline() returns an empty string, | ||
# the end of the file has been reached, while a blank line is represented by '\n', a string | ||
# containing only a single newline. | ||
multi_line_file.seek(0) | ||
|
||
assert multi_line_file.readline() == 'first line\n' | ||
assert multi_line_file.readline() == 'second line\n' | ||
assert multi_line_file.readline() == 'third line' | ||
assert multi_line_file.readline() == '' | ||
|
||
multi_line_file.close() | ||
binary_file.close() | ||
with open('src/files/binary_file', 'r') as binary_file: | ||
# To read a file’s contents, call f.read(size), which reads some quantity of data and returns | ||
# it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric | ||
# argument. When size is omitted or negative, the entire contents of the file will be read and | ||
# returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, | ||
# at most size bytes are read and returned. If the end of the file has been reached, f.read() | ||
# will return an empty string (''). | ||
read_data = multi_line_file.read() | ||
|
||
# pylint: disable=duplicate-code | ||
assert read_data == 'first line\nsecond line\nthird line' | ||
|
||
# To change the file object’s position, use f.seek(offset, from_what). The position is computed | ||
# from adding offset to a reference point; the reference point is selected by the from_what | ||
# argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current | ||
# file position, and 2 uses the end of the file as the reference point. from_what can be omitted | ||
# and defaults to 0, using the beginning of the file as the reference point. | ||
assert binary_file.seek(0) == 0 # Go to the 0th byte in the file | ||
assert binary_file.seek(6) == 6 # Go to the 6th byte in the file | ||
|
||
assert binary_file.read(1) == '6' | ||
|
||
# f.readline() reads a single line from the file; a newline character (\n) is left at the end | ||
# of the string, and is only omitted on the last line of the file if the file doesn’t end in a | ||
# newline. This makes the return value unambiguous; if f.readline() returns an empty string, | ||
# the end of the file has been reached, while a blank line is represented by '\n', a string | ||
# containing only a single newline. | ||
multi_line_file.seek(0) | ||
|
||
assert multi_line_file.readline() == 'first line\n' | ||
assert multi_line_file.readline() == 'second line\n' | ||
assert multi_line_file.readline() == 'third line' | ||
assert multi_line_file.readline() == '' | ||
|
||
multi_line_file.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_file_methods
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
)
# Open files without using 'with' statement. | ||
file = open('src/files/multi_line_file.txt', 'r') | ||
|
||
assert not file.closed | ||
|
||
read_data = file.read() | ||
with open('src/files/multi_line_file.txt', 'r') as file: | ||
assert not file.closed | ||
|
||
assert read_data == ( | ||
'first line\n' | ||
'second line\n' | ||
'third line' | ||
) | ||
read_data = file.read() | ||
|
||
file.close() | ||
assert read_data == ( | ||
'first line\n' | ||
'second line\n' | ||
'third line' | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_files_open
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
)
This removes the following comments ( why? ):
# Open files without using 'with' statement.
return ham + ' and ' + eggs | ||
return f'{ham} and {eggs}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function breakfast
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
# Addition. | ||
assert 5 + 3 == 8 | ||
|
||
# Subtraction. | ||
assert 5 - 3 == 2 | ||
|
||
# Multiplication. | ||
assert 5 * 3 == 15 | ||
assert isinstance(5 * 3, int) | ||
|
||
# Division. | ||
# Result of division is float number. | ||
assert 5 / 3 == 1.6666666666666667 | ||
assert 8 / 4 == 2 | ||
assert 5 == 1.6666666666666667 * 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_arithmetic_operators
refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass
) - Remove
assert True
statements (remove-assert-true
) - Simplify numeric comparison (
simplify-numeric-comparison
) - Simplify x == x -> True and x != x -> False (
equality-identity
)
This removes the following comments ( why? ):
# Subtraction.
# Multiplication.
# Addition.
|
||
# Assignment: = | ||
number = 5 | ||
assert number == 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_assignment_operator
refactored with the following changes:
- Inline variable that is only used once (
inline-variable
) - Remove redundant pass statement (
remove-redundant-pass
) - Remove
assert True
statements (remove-assert-true
) - Simplify x == x -> True and x != x -> False (
equality-identity
)
This removes the following comments ( why? ):
# Assignment: =
# Assignment: += | ||
number = 5 | ||
number += 3 | ||
number = 5 + 3 | ||
assert number == 8 | ||
|
||
# Assignment: -= | ||
number = 5 | ||
number -= 3 | ||
number = 5 - 3 | ||
assert number == 2 | ||
|
||
# Assignment: *= | ||
number = 5 | ||
number *= 3 | ||
number = 5 * 3 | ||
assert number == 15 | ||
|
||
# Assignment: /= | ||
number = 8 | ||
number /= 4 | ||
number = 8 / 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_augmented_assignment_operators
refactored with the following changes:
- Replace assignment and augmented assignment with single assignment (
merge-assign-and-aug-assign
)
This removes the following comments ( why? ):
# Assignment: -=
# Assignment: /=
# Assignment: +=
# Assignment: *=
# is | ||
# Returns true if both variables are the same object. | ||
|
||
# Example: | ||
# first_fruits_list and third_fruits_list are the same objects. | ||
assert first_fruits_list is third_fruits_list | ||
|
||
# is not | ||
# Returns true if both variables are not the same object. | ||
|
||
# Example: | ||
# first_fruits_list and second_fruits_list are not the same objects, even if they have | ||
# the same content | ||
assert first_fruits_list is not second_fruits_list | ||
|
||
# To demonstrate the difference between "is" and "==": this comparison returns True because | ||
# first_fruits_list is equal to second_fruits_list. | ||
assert first_fruits_list == second_fruits_list | ||
assert third_fruits_list is third_fruits_list | ||
assert third_fruits_list is not second_fruits_list | ||
assert third_fruits_list == second_fruits_list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_identity_operators
refactored with the following changes:
- Use previously assigned local variable (
use-assigned-variable
)
This removes the following comments ( why? ):
# Returns true if both variables are the same object.
# is not
# To demonstrate the difference between "is" and "==": this comparison returns True because
# is
# first_fruits_list and second_fruits_list are not the same objects, even if they have
# first_fruits_list is equal to second_fruits_list.
# Example:
# the same content
# first_fruits_list and third_fruits_list are the same objects.
# Returns true if both variables are not the same object.
assert not first_number == second_number | ||
assert first_number != second_number | ||
assert first_number != second_number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_logical_operators
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
)
Branch
master
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run:Help us improve this pull request!