Skip to content

Conversation

alanmcruickshank
Copy link
Member

@alanmcruickshank alanmcruickshank commented Sep 23, 2024

This follows on from #6240 and should support #6220.

This completes the job of making the whole of sqlfluff.core compliant with mypy --strict.

The majority of this is fairly uncontroversial and just fiddly type annotations. The only two more major changes are

  • A slight hack for the hookspec, which involved moving the types module up a level, and introducing a RuleType class as a base class for rules, but which only acts as a return type for the base get_rules() method. UPDATE: Ignore this, I think it's gross so just added an exception in the import linter to allow it. There's already two other exceptions in there for type checking.
  • A refactoring of get_context for the templaters, including a change for the jinja templater which use to override this method, and now implements it's own custom method which calls this method to allow it to change the type signature.

I've also added a bit more documentation around the py.typed file.

Copy link
Contributor

github-actions bot commented Sep 23, 2024

Coverage Results ✅

Name    Stmts   Miss  Cover   Missing
-------------------------------------
TOTAL   18366      0   100%

236 files skipped due to complete coverage.

@coveralls
Copy link

coveralls commented Sep 23, 2024

Coverage Status

coverage: 99.985%. remained the same
when pulling cff6b54 on ac/typing2
into 98cd54f on main.

Comment on lines 19 to 26
def get_rules(self) -> List[Type[RuleType]]:
"""Get plugin rules.

NOTE: While the type annotation for this method returns `Type[RuleType]`
all plugin implementations should instead import `sqlfluff.core.rules` and
return the more specific `Type[BaseRule]`. This base definition of the
PluginSpec cannot import that yet as it would cause a circular import.
"""
Copy link
Member Author

Choose a reason for hiding this comment

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

@WittierDinosaur - this is the only bit that feels a bit gross, but this is the least bad of a few choices. Those options are:

  1. (what I've done, which is) Have the plugin module describe something "rule-like" which get_rules() returns, and that be a parent class of BaseRule so that mypy is happy.
  2. We could have the type annotation here reference BaseRule directly, but that violates the lint-imports rule which says that the linter can't depend on rules (it must be the other way around only) - and at the moment, linter depends on config which depends on plugin. linter is likely always going to depend on config, and refactoring config so that it doesn't need to depend on plugin seems hard or even impossible.
  3. We could avoid the new RuleType class and just make this type hint really vague (i.e. List[Any] or List[Type[Any]]). You could argue that the current RuleType implementation isn't much better than this because the class doesn't actually define anything other than being a marker for inheritance, but it feels a little more descriptive than this.

What do you reckon?

Copy link
Member Author

Choose a reason for hiding this comment

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

for context, the problem we're trying to solve is:

What should the type annotation on get_rules() be?

Strict mypy wanted it to have something more specific than just list.

Copy link
Member Author

@alanmcruickshank alanmcruickshank Sep 27, 2024

Choose a reason for hiding this comment

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

Ignore all of this. I think it's gross. I've just added an exception in the import linter instead and imported BaseRule for TYPE_CHECKING.

Comment on lines +35 to +38
# TODO: This type annotation could probably be more specific but that would
# require making the config info object something more like a namedTuple rather
# than a dict.
def get_configs_info(self) -> Dict[str, Dict[str, Any]]:
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not proposing that I resolve this TODO in this PR. It's more of a marker for later improvements. What I've done here is already better than what was here before.

@alanmcruickshank alanmcruickshank merged commit 8abc2f4 into main Sep 28, 2024
29 checks passed
@alanmcruickshank alanmcruickshank deleted the ac/typing2 branch September 28, 2024 17:52
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.

2 participants