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

Use iskeyword and str.isidentifier for "is FQCN" #73279

Merged

Conversation

webknjaz
Copy link
Member

SUMMARY

This is extracted from #72591.

ISSUE TYPE
  • Refactoring Pull Request
COMPONENT NAME

N/A

ADDITIONAL INFORMATION

N/A

N/A

@ansibot ansibot added affects_2.11 core_review In order to be merged, this PR must follow the core review workflow. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jan 18, 2021
@sivel
Copy link
Member

sivel commented Jan 18, 2021

We already have an isidentifier function in ansible. Located at ansible.utils.vars.isidentifier

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Jan 18, 2021
@webknjaz
Copy link
Member Author

We already have an isidentifier function in ansible. Located at ansible.utils.vars.isidentifier

I've taken a prompt look and it seems that this helper doesn't replicate the stdlib behavior so I'm not sure if we want to reuse it for FQCNs.

@samdoran samdoran added the ci_verified Changes made in this PR are causing tests to fail. label Jan 18, 2021
@Shrews Shrews removed the needs_triage Needs a first human triage before being processed. label Jan 19, 2021
Copy link
Member

@nitzmahone nitzmahone left a comment

Choose a reason for hiding this comment

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

I'm all for tightening this up to delegate to stdlib, but a few questions:

  • why expose this as public API (and esp in module_utils, since modules should never need it)?
  • should we just wait for 2.12 so we don't need the 2.x fallback?
  • if we are doing for 2.11, the regex needs to be compiled- this gets called A LOT

@webknjaz
Copy link
Member Author

@nitzmahone

  • why expose this as public API (and esp in module_utils, since modules should never need it)?

Honestly, I wasn't sure where to put it, just wanted a separate compat module. The dependency resolver rewrite (#72591) will need it, ideally through calling is_valid_collection_name, though. Any suggestions?

  • should we just wait for 2.12 so we don't need the 2.x fallback?

I wanted to move it out of #72591 so I thought it'd be better to do it sooner than later.

  • if we are doing for 2.11, the regex needs to be compiled- this gets called A LOT

This is probably unnecessary — it's already cached internally: https://github.com/python/cpython/blob/c92cd0f/Lib/re.py#L285 / https://github.com/python/cpython/blob/2.7/Lib/re.py#L234-L241.

@nitzmahone
Copy link
Member

Unless you've got an immediate concrete need for it independently of is_valid_collection_name, I'd rather keep all that inside _collection_finder for now rather than making a whole new module_utils compat module "just in case". The compile should also definitely be explicit, since the caching is an implementation detail (that's also dependent on the flags). If it's already cached, great, compile will no-op, but otherwise we want to make darn sure it's compiled.

@mattclay
Copy link
Member

There's no guarantee the compiled pattern will remain in the cache. The docs state:

The compiled versions of the most recent patterns passed to re.compile() and the module-level matching functions are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions.

Looking at the source for both CPython 2.7 and 3.9, the LRU cache size is fixed at 512 entries. So while it's likely to stay cached, we shouldn't assume that.

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jan 27, 2021
@nitzmahone
Copy link
Member

@webknjaz if you don't want to hold up #72591 for this, no worries, but I do like the overall change and agree that the code in galaxy-cli should share the impl with the loader, so if #72591 gets merged first, maybe just add the impl-sharing change to galaxy-cli here?

@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. core_review In order to be merged, this PR must follow the core review workflow. labels Jan 29, 2021
@webknjaz webknjaz force-pushed the refactoring/is_valid_collection_name branch from 2a4a868 to bcf1e04 Compare January 29, 2021 18:46
@webknjaz
Copy link
Member Author

@nitzmahone @mattclay I moved the shim into _collection_finder and made it use re.compile().

@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 29, 2021

return all(
# NOTE: keywords and identifiers are different in differnt Pythons
not iskeyword(ns_or_name) and is_python_identifier(ns_or_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

This could use a changelog. We might also want to document this in the developing_collections page or improve error messages to give a better clue about what might be wrong.

ansible-galaxy:

ERROR! Invalid collection name 'def.coll', name must be in the format <namespace>.<collection>. 
Please make sure namespace and collection name contains characters from [a-zA-Z0-9_] only.

Using a now-invalid collection:

[WARNING]: errors were encountered during the plugin load for def.coll.my_module: ['invalid collection name (must be of the form namespace.collection): def.coll']

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough. But I think that changing the error message should go to a separate PR. The resolver already uses this code in one place anyway...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, but in a very limited way. You could initialize + build + install the tarfile + use it, and it would work. I'm fine with changing the error later though.

@nitzmahone nitzmahone merged commit f327e65 into ansible:devel Feb 12, 2021
@ansible ansible locked and limited conversation to collaborators Mar 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.11 core_review In order to be merged, this PR must follow the core review workflow. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants