Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Make rule matching fast #3

Open
bsamuel-ui opened this issue Feb 11, 2019 · 0 comments
Open

Make rule matching fast #3

bsamuel-ui opened this issue Feb 11, 2019 · 0 comments

Comments

@bsamuel-ui
Copy link
Contributor

The rules are very simple, but require a bunch of redundant checks. More importantly, we can't introspect them to determine if one rule is shadowing another.

While writing up the standard rules, I've observed several patterns:

  1. type == X all the atom types
  2. issubclass(type, X) enums must use issubclass; atoms presently use this as well.
  3. type.__origin__ == X the generic types.
  4. issubclass(type.__origin__, X) the subclass variant is not used.
  5. hasattr(type, X) - used to match attrs and dataclass
  6. type.__args__ =~ [X, NoneType] used to match Optional as it's a special case of Union.

The concrete and generic types are not entirely exclusive because user classes can subclass some generic types. (You can subclass something like typing.List[foo], but not typing.Union.)

If we simply had a mapping of known atom types, it's easy enough to look them up. And if you specify two atom types, this should raise an error as one is entirely shadowing another.

An issubclass test can walk .mro() until a base is a match in the mapping.

Each pattern represents a phase, and each phase has a "least-order." If a match is found that is greater than another phase's least-order, that phase can be disregarded and the search is complete.

Making type matches work with subclass matches mean we want is-proper-subclass-of rather than issuclass's improper semantics so there's no overlap.

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

No branches or pull requests

1 participant