8000 FEATURE: Force regex-matched arguments to be named by frank-lenormand · Pull Request #1243 · behave/behave · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

FEATURE: Force regex-matched arguments to be named #1243

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

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

Conversation

frank-lenormand
Copy link

Hi!

In my step matchers that are backed with re, I name all arguments explicitly, i.e. (?P<myarg>\w+).

It is consistent with match naming with the other matchers ({myarg}), and I don’t have to worry about the order of the arguments in the callback for the matcher.

And in the case when many optional arguments are to be matched, the current version of the regex matcher in Behave forces me to use the uncapture sequence a lot, for example:

use_step_matcher("re")
@given(
  r"A file named (?P<filename>)"
  r"(?: with owner (?P<owner>\w+))?" 
  r"(?: with group (?P<group>\w+))?" 
  r"(?: with mask (?P<mask>\d+))?" 
[…]
)
def given_a_file(context: Context, filename: str, owner: str, group: str, mask: str):
  pass

Removing the ?: sequences in the above would lead to an error similar to:

TypeError: run() got multiple values for argument 'filename'

With the present patch, only arguments named explicitly using the ?P<argument> construct would be passed to the handler, simplifying the code and making it more readable:

use_step_matcher("re")
@given(
  r"A file named (?P<filename>)"
  r"( with owner (?P<owner>\w+))?" 
  r"( with group (?P<group>\w+))?" 
  r"( with mask (?P<mask>\d+))?" 
[…]
)
def given_a_file(context: Context, filename: str, owner: str, group: str, mask: str):
  pass

Tell me what you think, HTH!

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.

1 participant
0