Skip to content

Commit f78d1fd

Browse files
authored
Fix another crash with report generation on namespace packages (#14063)
Fixes #14046. Similar to #13733 Best reviewed with hide whitespace.
1 parent 70e544b commit f78d1fd

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

Diff for: mypy/report.py

+41-44
Original file line numberDiff line numberDiff line change
@@ -637,51 +637,48 @@ def on_file(
637637
etree.SubElement(class_element, "methods")
638638
lines_element = etree.SubElement(class_element, "lines")
639639

640-
with tokenize.open(path) as input_file:
641-
class_lines_covered = 0
642-
class_total_lines = 0
643-
for lineno, _ in enumerate(input_file, 1):
644-
status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
645-
hits = 0
646-
branch = False
647-
if status == stats.TYPE_EMPTY:
648-
continue
649-
class_total_lines += 1
650-
if status != stats.TYPE_ANY:
651-
class_lines_covered += 1
652-
hits = 1
653-
if status == stats.TYPE_IMPRECISE:
654-
branch = True
655-
file_info.counts[status] += 1
656-
line_element = etree.SubElement(
657-
lines_element,
658-
"line",
659-
branch=str(branch).lower(),
660-
hits=str(hits),
661-
number=str(lineno),
662-
precision=stats.precision_names[status],
663-
)
664-
if branch:
665-
line_element.attrib["condition-coverage"] = "50% (1/2)"
666-
class_element.attrib["branch-rate"] = "0"
667-
class_element.attrib["line-rate"] = get_line_rate(
668-
class_lines_covered, class_total_lines
640+
class_lines_covered = 0
641+
class_total_lines = 0
642+
for lineno, _ in iterate_python_lines(path):
643+
status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
644+
hits = 0
645+
branch = False
646+
if status == stats.TYPE_EMPTY:
647+
continue
648+
class_total_lines += 1
649+
if status != stats.TYPE_ANY:
650+
class_lines_covered += 1
651+
hits = 1
652+
if status == stats.TYPE_IMPRECISE:
653+
branch = True
654+
file_info.counts[status] += 1
655+
line_element = etree.SubElement(
656+
lines_element,
657+
"line",
658+
branch=str(branch).lower(),
659+
hits=str(hits),
660+
number=str(lineno),
661+
precision=stats.precision_names[status],
669662
)
670-
# parent_module is set to whichever module contains this file. For most files, we want
671-
# to simply strip the last element off of the module. But for __init__.py files,
672-
# the module == the parent module.
673-
parent_module = file_info.module.rsplit(".", 1)[0]
674-
if file_info.name.endswith("__init__.py"):
675-
parent_module = file_info.module
676-
677-
if parent_module not in self.root_package.packages:
678-
self.root_package.packages[parent_module] = CoberturaPackage(parent_module)
679-
current_package = self.root_package.packages[parent_module]
680-
packages_to_update = [self.root_package, current_package]
681-
for package in packages_to_update:
682-
package.total_lines += class_total_lines
683-
package.covered_lines += class_lines_covered
684-
current_package.classes[class_name] = class_element
663+
if branch:
664+
line_element.attrib["condition-coverage"] = "50% (1/2)"
665+
class_element.attrib["branch-rate"] = "0"
666+
class_element.attrib["line-rate"] = get_line_rate(class_lines_covered, class_total_lines)
667+
# parent_module is set to whichever module contains this file. For most files, we want
668+
# to simply strip the last element off of the module. But for __init__.py files,
669+
# the module == the parent module.
670+
parent_module = file_info.module.rsplit(".", 1)[0]
671+
if file_info.name.endswith("__init__.py"):
672+
parent_module = file_info.module
673+
674+
if parent_module not in self.root_package.packages:
675+
self.root_package.packages[parent_module] = CoberturaPackage(parent_module)
676+
current_package = self.root_package.packages[parent_module]
677+
packages_to_update = [self.root_package, current_package]
678+
for package in packages_to_update:
679+
package.total_lines += class_total_lines
680+
package.covered_lines += class_lines_covered
681+
current_package.classes[class_name] = class_element
685682

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

0 commit comments

Comments
 (0)