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

93 change regex command #94

Closed
wants to merge 6 commits into from
Closed

93 change regex command #94

wants to merge 6 commits into from

Conversation

MattPrit
Copy link
Collaborator

@MattPrit MattPrit commented Sep 5, 2022

Requires #89
Fixes #92 and #93
Required for #90

  • Allows for RegexCommand to parse from data of type AnyStr rather than just bytes.
  • RegexCommand.parse now also returns the start and end indices of the match within the message, as well as the length of the message matched against. This gives enough information for MultiCommandInterprerter to strip off a matched command and for CommandInterpreter to check for a full match.
  • CommandInterpreter.handle logic updated to use the new return signature.
  • Tests added for RegexCommand and CommandInterpreter

@codecov
Copy link

codecov bot commented Sep 5, 2022

Codecov Report

Merging #94 (0ca0d1f) into master (f4b3625) will increase coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #94      +/-   ##
==========================================
+ Coverage   98.58%   98.62%   +0.04%     
==========================================
  Files          69       69              
  Lines        2117     2181      +64     
==========================================
+ Hits         2087     2151      +64     
  Misses         30       30              
Impacted Files Coverage Δ
...apters/interpreters/command/command_interpreter.py 100.00% <100.00%> (ø)
...kit/adapters/interpreters/command/regex_command.py 100.00% <100.00%> (ø)
tickit/adapters/interpreters/utils.py 100.00% <0.00%> (ø)
tickit/adapters/interpreters/wrappers/__init__.py 100.00% <0.00%> (ø)
tickit/__init__.py
...ers/interpreters/wrappers/splitting_interpreter.py 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@MattPrit MattPrit mentioned this pull request Sep 5, 2022
@MattPrit MattPrit marked this pull request as ready for review September 7, 2022 13:06
Copy link
Member

@garryod garryod left a comment

Choose a reason for hiding this comment

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

I reckon we should merge #91 first to forgo the complexities of performing str<->bytes conversion for regex

@@ -12,16 +20,17 @@ class Command(Protocol):

#: A flag which indicates whether calling of the method should trigger an interrupt
interrupt: bool
# format: Optional[str] = None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# format: Optional[str] = None


@abstractmethod
def parse(self, data: bytes) -> Optional[Sequence[AnyStr]]:
def parse(self, data: AnyStr) -> Optional[Tuple[Sequence[AnyStr], int, int, int]]:
Copy link
Member

Choose a reason for hiding this comment

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

Feels like Tuple[Sequence[AnyStr], int, int, int] should be given a NewType declaration or even a dataclass

message_end,
) = parse_result
if match_end != message_end:
# We want the whole (formatted) message to match a command
Copy link
Member

Choose a reason for hiding this comment

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

Documentation of the whole / complete would be good to have in the class docstring

Comment on lines 116 to 122
# can we use signature instead to more cleverly do conversions? i.e. if
# type hints aren't available just pass on with no conversion? Otherwise
# we can get unhelpful error messages.
# Also, should we deal with str/bytes conversions seperately?
# Create a mapping function that does conversion rather than argtype(arg)?
# It would take args and inspect.Parameters as parameters?
# Is type hints enough? - doesn't know about non-hinted vars
Copy link
Member

Choose a reason for hiding this comment

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

This is definitely worth investigating at some point. My current thoughts are:

  • The case of no type hints should be accounted for, either by raising an error or dumping the raw AnyStr on the user
  • Due to the complexity of bytes<->string it makes most sense to dump the raw AnyStr on the user and let them deal with it, the complexity of passing through encodings / decodings seems unnecessary when the user can do it in their endpoint.
  • A mapping function would be nice if we could come up with one that satisfied users needs across the board, but I frankly don't see that happening in a way that's better than using the default casts of the types

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think discussions like this are better had in new issues than in code comments/PRs. Can we remove the code in the comment and make a new issue?

@@ -36,24 +36,50 @@ def __call__(self, func: Callable) -> Callable:
setattr(func, "__command__", self)
return func

def parse(self, data: bytes) -> Optional[Sequence[AnyStr]]:
def parse(self, data: AnyStr) -> Optional[Tuple[Sequence[AnyStr], int, int, int]]:
Copy link
Member

Choose a reason for hiding this comment

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

This can be drastically simplified by removing all encoding / decoding if we merge #91 first

@abbiemery
Copy link
Collaborator

Closing this as not planned. When it becomes desired again raise a new issue, linked to this #177.

@abbiemery abbiemery closed this Aug 8, 2023
@abbiemery abbiemery deleted the 93_change_regex_command branch August 8, 2023 15:42
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.

Allow Command to parse AnyStr
4 participants