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

Fix another crash with report generation on namespace packages #14063

Merged
merged 2 commits into from Nov 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 41 additions & 44 deletions mypy/report.py
Expand Up @@ -637,51 +637,48 @@ def on_file(
etree.SubElement(class_element, "methods")
lines_element = etree.SubElement(class_element, "lines")

with tokenize.open(path) as input_file:
class_lines_covered = 0
class_total_lines = 0
for lineno, _ in enumerate(input_file, 1):
status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
hits = 0
branch = False
if status == stats.TYPE_EMPTY:
continue
class_total_lines += 1
if status != stats.TYPE_ANY:
class_lines_covered += 1
hits = 1
if status == stats.TYPE_IMPRECISE:
branch = True
file_info.counts[status] += 1
line_element = etree.SubElement(
lines_element,
"line",
branch=str(branch).lower(),
hits=str(hits),
number=str(lineno),
precision=stats.precision_names[status],
)
if branch:
line_element.attrib["condition-coverage"] = "50% (1/2)"
class_element.attrib["branch-rate"] = "0"
class_element.attrib["line-rate"] = get_line_rate(
class_lines_covered, class_total_lines
class_lines_covered = 0
class_total_lines = 0
for lineno, _ in iterate_python_lines(path):
status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
hits = 0
branch = False
if status == stats.TYPE_EMPTY:
continue
class_total_lines += 1
if status != stats.TYPE_ANY:
class_lines_covered += 1
hits = 1
if status == stats.TYPE_IMPRECISE:
branch = True
file_info.counts[status] += 1
line_element = etree.SubElement(
lines_element,
"line",
branch=str(branch).lower(),
hits=str(hits),
number=str(lineno),
precision=stats.precision_names[status],
)
# parent_module is set to whichever module contains this file. For most files, we want
# to simply strip the last element off of the module. But for __init__.py files,
# the module == the parent module.
parent_module = file_info.module.rsplit(".", 1)[0]
if file_info.name.endswith("__init__.py"):
parent_module = file_info.module

if parent_module not in self.root_package.packages:
self.root_package.packages[parent_module] = CoberturaPackage(parent_module)
current_package = self.root_package.packages[parent_module]
packages_to_update = [self.root_package, current_package]
for package in packages_to_update:
package.total_lines += class_total_lines
package.covered_lines += class_lines_covered
current_package.classes[class_name] = class_element
if branch:
line_element.attrib["condition-coverage"] = "50% (1/2)"
class_element.attrib["branch-rate"] = "0"
class_element.attrib["line-rate"] = get_line_rate(class_lines_covered, class_total_lines)
# parent_module is set to whichever module contains this file. For most files, we want
# to simply strip the last element off of the module. But for __init__.py files,
# the module == the parent module.
parent_module = file_info.module.rsplit(".", 1)[0]
if file_info.name.endswith("__init__.py"):
parent_module = file_info.module

if parent_module not in self.root_package.packages:
self.root_package.packages[parent_module] = CoberturaPackage(parent_module)
current_package = self.root_package.packages[parent_module]
packages_to_update = [self.root_package, current_package]
for package in packages_to_update:
package.total_lines += class_total_lines
package.covered_lines += class_lines_covered
current_package.classes[class_name] = class_element

def on_finish(self) -> None:
self.root.attrib["line-rate"] = get_line_rate(
Expand Down