Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly match package/module names in import hook (#144)
The previously used regular expression tried to handle both exact matches and and prefix matches in one go, using this approach: re.compile(r'^%s\.?' % pkg) However, this is incorrect, since the literal dot is optional in the pattern, causing longer matches to also get included. For example, ‘foo’ should match ‘foo’ and ‘foo.bar’, but it also incorrectly matches ‘foobar’: >>> re.compile(r'^foo\.?').match('foobar') <_sre.SRE_Match object; span=(0, 3), match='foo'> In practice, a command like this (using the pytest plugin as an example) is supposed to check the ‘flask’ package and any modules below it: pytest --typeguard-packages=flask ... but in reality it also checks other packages, such as ‘flask_sqlalchemy’ and ‘flask_redis’, if those happen to be installed. This can be easily fixed by not using regular expression, but simple string matching instead.
- Loading branch information