-
Notifications
You must be signed in to change notification settings - Fork 0
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
Sourcery refactored develop branch #1
base: develop
Are you sure you want to change the base?
Conversation
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.
Due to GitHub API limits, only the first 60 comments can be shown.
# if there is already an entry in this account then just add it | ||
# to that entry | ||
same_head = check_if_in_list(entry, merged_gl_map) | ||
if same_head: | ||
if same_head := check_if_in_list(entry, merged_gl_map): |
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 merge_similar_entries
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
This removes the following comments ( why? ):
# to that entry
# if there is already an entry in this account then just add it
billing_address_field = "customer_address" if party_type == "Lead" \ | ||
else party_type.lower() + "_address" | ||
billing_address_field = ( | ||
"customer_address" | ||
if party_type == "Lead" | ||
else f"{party_type.lower()}_address" | ||
) |
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 set_address_details
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
if party.get("default_" + f): | ||
out[f] = party.get("default_" + f) | ||
if party.get(f"default_{f}"): | ||
out[f] = party.get(f"default_{f}") |
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 set_other_values
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
out = { | ||
return { | ||
party_type.lower(): party, | ||
account_fieldname : account, | ||
"due_date": get_due_date(posting_date, party, party_type, account, company) | ||
account_fieldname: account, | ||
"due_date": get_due_date( | ||
posting_date, party, party_type, account, company | ||
), | ||
} | ||
return out |
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 set_account_and_due_date
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if not frappe.db.exists("Account", (party.strip() + " - " + company_details.abbr)): | ||
if not frappe.db.exists( | ||
"Account", f"{party.strip()} - {company_details.abbr}" | ||
): |
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 create_party_account
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
condition = "" | ||
if not self.include_reconciled_entries: | ||
condition = "and ifnull(clearance_date, '') in ('', '0000-00-00')" | ||
|
||
|
||
condition = ( | ||
"" | ||
if self.include_reconciled_entries | ||
else "and ifnull(clearance_date, '') in ('', '0000-00-00')" | ||
) |
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 BankReconciliation.get_details
refactored with the following changes:
- Move setting of default value for variable into
else
branch (introduce-default-else
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
) - Replace if statement with if expression (
assign-if-exp
)
idx =1 | ||
for m in month_list: | ||
for idx, m in enumerate(month_list, start=1): | ||
mnth = self.append('budget_distribution_details') | ||
mnth.month = m | ||
mnth.idx = idx | ||
idx += 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 BudgetDistribution.get_months
refactored with the following changes:
- Replace manual loop counter with call to enumerate (
convert-to-enumerate
)
total = sum([flt(d.percentage_allocation) for d in self.get("budget_distribution_details")]) | ||
total = sum( | ||
flt(d.percentage_allocation) | ||
for d in self.get("budget_distribution_details") | ||
) |
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 BudgetDistribution.validate
refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator
)
frappe.throw("C-form is not applicable for Invoice: %s" % d.invoice_no) | ||
frappe.throw(f"C-form is not applicable for Invoice: {d.invoice_no}") |
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 CForm.validate
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
inv = [d.invoice_no for d in self.get('invoice_details')] | ||
if inv: | ||
frappe.db.sql("""update `tabSales Invoice` set c_form_no=%s, modified=%s where name in (%s)""" % | ||
('%s', '%s', ', '.join(['%s'] * len(inv))), tuple([self.name, self.modified] + inv)) | ||
if inv := [d.invoice_no for d in self.get('invoice_details')]: | ||
frappe.db.sql( | ||
f"""update `tabSales Invoice` set c_form_no=%s, modified=%s where name in ({', '.join(['%s'] * len(inv))})""", | ||
tuple([self.name, self.modified] + inv), | ||
) |
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 CForm.set_cform_in_sales_invoices
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
) - Simplify unnecessary nesting, casting and constant values in f-strings (
simplify-fstring-formatting
)
total = sum([flt(d.grand_total) for d in self.get('invoice_details')]) | ||
total = sum(flt(d.grand_total) for d in self.get('invoice_details')) |
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 CForm.set_total_invoiced_amount
refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator
)
|
||
from erpnext.accounts.doctype.chart_of_accounts.charts.account_properties import account_properties | ||
|
||
if chart: | ||
accounts = [] | ||
|
||
def _import_accounts(children, parent): | ||
for child in children: | ||
account_name = child.get("name") | ||
account_name_in_db = unidecode(account_name.strip().lower()) | ||
|
||
if account_name_in_db in accounts: | ||
count = accounts.count(account_name_in_db) | ||
account_name = account_name + " " + cstr(count) | ||
account_name = f"{account_name} {cstr(count)}" | ||
|
||
child.update(account_properties.get(chart.get("name"), {}).get(account_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 ChartofAccounts.create_accounts
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
default_types_root = [] | ||
for file in ["data_account_type.xml"]: | ||
default_types_root.append(ET.parse(os.path.join(path, "account", "data", | ||
"data_account_type.xml")).getroot()) | ||
default_types_root = [ | ||
ET.parse( | ||
os.path.join(path, "account", "data", "data_account_type.xml") | ||
).getroot() | ||
for _ in ["data_account_type.xml"] | ||
] |
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 get_default_account_types
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Replace unused for index with underscore (
for-index-underscore
)
node_id = prefix + "." + node.get("id") if prefix else node.get("id") | ||
|
||
node_id = f"{prefix}." + node.get("id") if prefix else node.get("id") | ||
types[node_id] = data | ||
|
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 get_account_types
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
self.name = self.cost_center_name.strip() + ' - ' + \ | ||
frappe.db.get_value("Company", self.company, "abbr") | ||
self.name = f'{self.cost_center_name.strip()} - ' + frappe.db.get_value( | ||
"Company", self.company, "abbr" | ||
) |
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 CostCenter.autoname
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
import test_records as jv_test_records | ||
import test_records as jv_test_records | ||
from erpnext.selling.doctype.sales_order.test_sales_order \ | ||
import test_records as so_test_records | ||
import test_records as so_test_records | ||
from erpnext.buying.doctype.purchase_order.test_purchase_order \ | ||
import test_records as po_test_records | ||
import test_records as po_test_records | ||
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice \ | ||
import test_records as si_test_records | ||
import test_records as si_test_records | ||
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice \ | ||
import test_records as pi_test_records | ||
import test_records as pi_test_records |
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 TestPaymentTool.test_make_journal_voucher
refactored with the following changes:
- Merge dictionary updates via the union operator (
dict-assign-update-to-union
)
res = frappe.db.sql("""select name, user from `tabPOS Setting` | ||
if res := frappe.db.sql( | ||
"""select name, user from `tabPOS Setting` | ||
where ifnull(user, '') = %s and name != %s and company = %s""", | ||
(self.user, self.name, self.company)) | ||
if res: | ||
(self.user, self.name, self.company), | ||
): |
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 POSSetting.check_for_duplicate
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
if not include_current_pos: | ||
condition = " where name != '%s'" % self.name.replace("'", "\'") | ||
else: | ||
condition = "" | ||
|
||
condition = ( | ||
"" | ||
if include_current_pos | ||
else " where name != '%s'" % self.name.replace("'", "\'") | ||
) |
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 POSSetting.set_defaults
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
)
if not args.item_group: | ||
frappe.throw(_("Item Group not mentioned in item master for item {0}").format(args.item_code)) | ||
if not args.item_group: | ||
frappe.throw(_("Item Group not mentioned in item master for item {0}").format(args.item_code)) | ||
|
||
if args.customer and not (args.customer_group and args.territory): | ||
customer = frappe.db.get_value("Customer", args.customer, ["customer_group", "territory"]) | ||
if customer: | ||
if customer := frappe.db.get_value( | ||
"Customer", args.customer, ["customer_group", "territory"] | ||
): | ||
args.customer_group, args.territory = customer | ||
|
||
elif args.supplier and not args.supplier_type: | ||
args.supplier_type = frappe.db.get_value("Supplier", args.supplier, "supplier_type") | ||
|
||
pricing_rules = get_pricing_rules(args) | ||
pricing_rule = filter_pricing_rules(args, pricing_rules) | ||
|
||
if pricing_rule: | ||
if pricing_rule := filter_pricing_rules(args, pricing_rules): |
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 get_pricing_rule_for_item
refactored with the following changes:
- Hoist conditional out of nested conditional (
hoist-if-from-if
) - Use named expression to simplify assignment and conditional [×2] (
use-named-expression
)
condition = " ifnull("+field+", '') in ('" + \ | ||
"', '".join([d.replace("'", "\\'").replace('"', '\\"') for d in parent_groups])+"')" | ||
condition = ( | ||
f" ifnull({field}, '') in ('" | ||
+ "', '".join( | ||
[d.replace("'", "\\'").replace('"', '\\"') for d in parent_groups] | ||
) | ||
) + "')" | ||
return condition | ||
|
||
|
||
|
||
conditions = "" | ||
for field in ["company", "customer", "supplier", "supplier_type", "campaign", "sales_partner"]: | ||
if args.get(field): | ||
conditions += " and ifnull("+field+", '') in (%("+field+")s, '')" | ||
conditions += f" and ifnull({field}, '') in (%({field})s, '')" | ||
else: | ||
conditions += " and ifnull("+field+", '') = ''" | ||
conditions += f" and ifnull({field}, '') = ''" | ||
|
||
for parenttype in ["Customer Group", "Territory"]: | ||
group_condition = _get_tree_conditions(parenttype) | ||
if group_condition: | ||
conditions += " and " + group_condition | ||
conditions += f" and {group_condition}" |
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 get_pricing_rules
refactored with the following changes:
- Use f-string instead of string concatenation [×10] (
use-fstring-for-concatenation
)
max_priority = max([cint(p.priority) for p in pricing_rules]) | ||
if max_priority: | ||
if max_priority := max(cint(p.priority) for p in pricing_rules): |
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 filter_pricing_rules
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
) - Replace unneeded comprehension with generator [×2] (
comprehension-to-generator
)
all_rules_same = True | ||
val = [pricing_rules[0][k] for k in fields] | ||
for p in pricing_rules[1:]: | ||
if val != [p[k] for k in fields]: | ||
all_rules_same = False | ||
break | ||
|
||
return all_rules_same | ||
return all(val == [p[k] for k in fields] for p in pricing_rules[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 if_all_rules_same
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Use any() instead of for loop (
use-any
) - Invert any/all to simplify comparisons (
invert-any-all
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if d.item_code: # extra condn coz item_code is not mandatory in PV | ||
if frappe.db.get_value("Item", d.item_code, "is_purchase_item") != 'Yes': | ||
msgprint(_("Item {0} is not Purchase Item").format(d.item_code), raise_exception=True) | ||
if ( | ||
d.item_code | ||
and frappe.db.get_value("Item", d.item_code, "is_purchase_item") != 'Yes' | ||
): | ||
msgprint(_("Item {0} is not Purchase Item").format(d.item_code), raise_exception=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 PurchaseInvoice.check_active_purchase_items
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
)
This removes the following comments ( why? ):
# extra condn coz item_code is not mandatory in PV
if (acc_head and cstr(acc_head[0][0]) != cstr(self.supplier)) or (not acc_head and (self.credit_to != cstr(self.supplier) + " - " + self.company_abbr)): | ||
if ( | ||
(acc_head and cstr(acc_head[0][0]) != cstr(self.supplier)) | ||
or not acc_head | ||
and self.credit_to != f"{cstr(self.supplier)} - {self.company_abbr}" | ||
): |
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 PurchaseInvoice.check_for_acc_head_of_supplier
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
if d.purchase_order and not d.purchase_order in check_list and not d.purchase_receipt: | ||
if ( | ||
d.purchase_order | ||
and d.purchase_order not in check_list | ||
and not d.purchase_receipt | ||
): | ||
check_list.append(d.purchase_order) | ||
stopped = frappe.db.sql("select name from `tabPurchase Order` where status = 'Stopped' and name = %s", d.purchase_order) | ||
if stopped: | ||
if stopped := frappe.db.sql( | ||
"select name from `tabPurchase Order` where status = 'Stopped' and name = %s", | ||
d.purchase_order, | ||
): |
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 PurchaseInvoice.check_for_stopped_status
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
for period in period_list: | ||
columns.append({ | ||
columns.extend( | ||
{ | ||
"fieldname": period.key, | ||
"label": period.label, | ||
"fieldtype": "Currency", | ||
"width": 150 | ||
}) | ||
|
||
"width": 150, | ||
} | ||
for period in period_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 get_columns
refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend
)
account_map = dict(((r.name, r) for r in frappe.db.sql("""select acc.name, | ||
account_map = { | ||
r.name: r | ||
for r in frappe.db.sql( | ||
"""select acc.name, | ||
supp.supplier_name, supp.name as supplier | ||
from `tabAccount` acc, `tabSupplier` supp | ||
where acc.master_type="Supplier" and supp.name=acc.master_name""", as_dict=1))) | ||
where acc.master_type="Supplier" and supp.name=acc.master_name""", | ||
as_dict=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 execute
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
) - Replace index in for loop with direct reference (
for-index-replacement
) - Replace range(0, x) with range(x) (
remove-zero-from-range
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
gl_entries = frappe.db.sql("""select * from `tabGL Entry` | ||
where docstatus < 2 %s order by posting_date, account""" % | ||
(conditions), tuple(supplier_accounts), as_dict=1) | ||
return gl_entries | ||
return frappe.db.sql( | ||
"""select * from `tabGL Entry` | ||
where docstatus < 2 %s order by posting_date, account""" | ||
% (conditions), | ||
tuple(supplier_accounts), | ||
as_dict=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 get_gl_entries
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
|
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 get_conditions
refactored with the following changes:
- Replace interpolated string formatting with f-string [×3] (
replace-interpolation-with-fstring
)
account_supplier_type_map = {} | ||
for each in frappe.db.sql("""select acc.name, supp.supplier_type from `tabSupplier` supp, | ||
`tabAccount` acc where supp.name = acc.master_name group by acc.name"""): | ||
account_supplier_type_map[each[0]] = each[1] | ||
|
||
return account_supplier_type_map | ||
return { | ||
each[0]: each[1] | ||
for each in frappe.db.sql( | ||
"""select acc.name, supp.supplier_type from `tabSupplier` supp, | ||
`tabAccount` acc where supp.name = acc.master_name group by acc.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 get_account_supplier_type_map
refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 0.04%.
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! |
Branch
develop
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
develop
branch, then run:Help us improve this pull request!