Skip to content

BodyView.blocks() and .attributes() are annotated as List[NodeView] #328

Open

Description

@livingstaccato

hcl2/query/body.py annotates both methods as returning List["NodeView"],
but each only ever appends the concrete subclass:

def blocks(self, block_type=None, *labels) -> List["NodeView"]:
    from hcl2.query.blocks import BlockView
    ...

blocks() yields only BlockView, attributes() only AttributeView, and
BlockView.body is always a BodyView. Callers under mypy --strict
therefore cannot reach block_type, labels, name_labels or
AttributeView.name without an isinstance narrowing or a cast, even though
the runtime type is never anything else. DocumentView and BlockView widen
the same way in their delegating methods.

Narrowing the annotations to List["BlockView"] and List["AttributeView"]
would make the query API usable from typed code as written. This is
annotation-only; no runtime behaviour changes, and the view classes stay
imported inside the method bodies, since that import cycle is real.

(Noticed while porting a downstream Terraform block reader onto hcl2.query —
the views are otherwise a very good fit, they replaced about fifty lines of
hand-rolled rule-tree walking.)



The investigation behind this report was produced with AI assistance, working on my behalf. Every reproduction, version comparison and measurement cited was executed rather than inferred; I reviewed it before filing.


This issue, and the investigation behind it, were produced by an AI assistant (Claude) working on behalf of the author. Please review with that provenance in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions