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

Adding typing to tree branches #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

chanind
Copy link

@chanind chanind commented Jan 2, 2024

This PR removes the Any from the Branch type, so trees are fully recursively typed.

This is a first step towards #129, but figured this would be a small enough self-contained chunk of work to check if this is on the right path.


Variable = str
Constant = Union[str, float, int, None] # None for missing values
Role = str # '' for anonymous relations
Symbol = str
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct name for what's in branch targets? It seemed like the target must always be a string if it's not a Node, with even number constants showing up as strings. Are there edge-cases of trees where this isn't correct?


# Tree types
Branch = Tuple[Role, Any]
Branch = Tuple[Role, Union[Symbol, "Node"]]
Node = Tuple[Variable, List[Branch]]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have an AMR tree that consists of just a single Variable with no branches?

@@ -178,7 +178,8 @@ def _interpret_node(t: Node, variables: Set[Variable], model: Model):
triples.append(triple)
epidata.append((triple, epis))
# nested nodes
else:
# mypy forgets that (Node ∨ Sym) ^ ¬Sym → Node
elif is_tgt_node(target):
Copy link
Author

@chanind chanind Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty awkward, and will technically reduce performance just to get typing to work. Annoyingly, Mypy Typeguard doesn't work the same as isinstance() (python/typing#1351). So, replacing is_tgt_symbol(target) above with isinstance(target, str) then allows Mypy to infer that it must a Node here and a Symbol above without needing an elif here. Not sure which solution is best, since the method is more clear what's going on compared to a naked isinstance.

Alternatively, we could just use cast to force Mypy to recognize the correct type 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant