Skip to content

Commit c4b999a

Browse files
committed
remove type annotations in ParseResult dataclass
1 parent 77dfc4a commit c4b999a

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

__init__.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ def __getitem__(self, k):
278278
def __repr__(self):
279279
return f'#{self.id}={self.type}({",".join(map(str, self.attributes))})'
280280

281+
@dataclass
282+
class ParseResult:
283+
header: dict
284+
entities: dict[int, list[entity_instance]]
285+
281286

282287
def create_step_entity(entity_tree):
283288
t = T(visit_tokens=True).transform(entity_tree)
@@ -352,7 +357,7 @@ def parse(
352357
with_progress=False,
353358
with_tree=True,
354359
only_header=False,
355-
):
360+
) -> ParseResult:
356361
if filename:
357362
assert not filecontent
358363
filecontent = builtins.open(filename, encoding=None).read()
@@ -388,7 +393,10 @@ def replace_fn(match):
388393

389394
header = dict(map(make_header_ent, header_tree.children[0].children))
390395
validate_header_fields(header)
391-
return header
396+
return ParseResult(
397+
header = header,
398+
entities = defaultdict(list)
399+
)
392400

393401

394402
instance_identifiers = []
@@ -431,7 +439,11 @@ def replace_fn(match):
431439
raise SyntaxError(filecontent, e)
432440

433441
if with_tree:
434-
return process_tree(filecontent, ast, with_progress)
442+
header, data = process_tree(filecontent, ast, with_progress)
443+
return ParseResult(
444+
header = header,
445+
entities = data
446+
)
435447
else:
436448
# process_tree() would take care of duplicate identifiers,
437449
# but we need to do it ourselves now using our rudimentary
@@ -448,8 +460,9 @@ class file:
448460
A somewhat compatible interface (but very limited) to ifcopenshell.file
449461
"""
450462

451-
def __init__(self, parse_outcomes):
452-
self.header_, self.data_ = parse_outcomes
463+
def __init__(self, result:ParseResult):
464+
self.header_ = result.header
465+
self.data_ = result.entities
453466

454467
@property
455468
def schema_identifier(self) -> str:
@@ -536,17 +549,4 @@ def by_type(self, type: str) -> list[entity_instance]:
536549

537550

538551
def open(fn, only_header: bool = False) -> file:
539-
if only_header: # Ensure consistent options
540-
parse_outcomes = parse(
541-
filename=fn,
542-
with_tree=True,
543-
only_header=True,
544-
)
545-
return file((parse_outcomes, defaultdict(list))) # data section is empty
546-
else:
547-
parse_outcomes = parse(
548-
filename=fn,
549-
with_tree=True,
550-
only_header=False,
551-
)
552-
return file(parse_outcomes)
552+
return file(parse(filename=fn, only_header=only_header))

test_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ def test_parse_features():
3232
f = open('fixtures/pass_1.ifc')
3333
assert f.by_id(1).id == 1
3434
assert f.by_id(1).type == 'IFCPERSON'
35+
assert f.data_[1][0].type == 'IFCPERSON'
3536
assert f.by_type('ifcperson')[0].id == 1
3637
assert f[1][0] is None
3738
assert f.header.file_description[0][0] == 'ViewDefinition [CoordinationView]'
39+
assert f.header_.get('FILE_DESCRIPTION')[0][0]
3840
assert f.by_type('ifcapplication')[1][2] == "Nested ' quotes"
3941

4042

0 commit comments

Comments
 (0)