sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.53k stars 66 forks source link

Qualified name resolution not working in rule tests #275

Closed bibz closed 1 year ago

bibz commented 2 years ago

Checklist

Description

According to the pattern syntax documentation, qualified name resolution should work out of the box.

Defining a sourcery configuration with the example rule from the documentation is not working, the IDE plugin (and the CLI as well) complains the tests do not pass.

# .sourcery.yaml
rules:
  - id: avoid-logging-debug
    description: Remove logging.debug calls from code
    pattern: logging.debug
    tests:
      # literal matches still happen, so you do not need to import `logging`
      # here:
      - match: logging.debug(DEBUG_MESSAGE)
      - no-match: logging.info(INFO_MESSAGE)
      # NOTE: the following test matches because of name resolution.
      # The name `lg` refers to `logging`, so `lg.debug` resolves to
      # `logging.debug`.
      - match: |
          import logging as lg

          lg.debug(DEBUG_MESSAGE)
      # NOTE: the following test matches because of name resolution.
      # The name `dbug` refers to `logging.debug` because of the aliased
      # import.
      - match: |
          import logging.debug as dbug

          dbug(DEBUG_MESSAGE)
      # NOTE: the following test matches because of name resolution.
      # The name `debug` refers to `logging.debug` because it was directly
      # imported from the module `logging`.
      - match: |
          from logging import debug

          debug(DEBUG_MESSAGE)
% sourcery review
Error loading config:
.sourcery.yaml:12:8-12:13 (rules -> 0 -> tests -> 2 -> match) Match example did not trigger
.sourcery.yaml:26:8-26:13 (rules -> 0 -> tests -> 4 -> match) Match example did not trigger

I would expect that the example rule from the documentation is supported by the tool.

Debug Information

IDE Version: PyCharm 2022.2.1 (Professional Edition) Sourcery Version: sourcery, version 0.12.7

Operating system and Version: Debian 11.4 (Linux 5.10.0-17-amd64 #1 SMP Debian 5.10.136-1 (2022-08-13) x86_64 GNU/Linux)

brendanator commented 2 years ago

Thanks for reporting this issue @bibz

I have literally just discovered this issue internally myself!

I have a fix for it which will be in the next release of Sourcery

brendanator commented 2 years ago

Note that this is only failing in the config. If you comment out those failing tests then it will work in the rest of your codebase

bibz commented 2 years ago

Note that this is only failing in the config. If you comment out those failing tests then it will work in the rest of your codebase

Ah, good to know! Indeed, as you mention, without the test my rule works :+1:

Hellebore commented 1 year ago

Resolved in 0.12.8