lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Lintr to enforce :: for packages

Open hhoeflin opened this issue 5 years ago • 11 comments

Hi,

I was wondering what you/the community thinks about a linter enforcing use of '::'. In python, code is often much easier to read as it is simpler to figure out where a function is coming from.

So the idea would be to have a linter that

  • reports any function from a package that is used without a '::'
    • this could be disabled by a comment along the lines of: 'from package import blah', i.e. similar to python, but based on a comment as it is not a language feature
    • for 'base' packages this could be relaxed or switched off, or generally switched off for a defined set of packages
  • functions used in a script that are from another script (e.g. inside a package) could also be declared in comments

Intent would be to increase general readability and encourage defensive programming (with the '::')

Thoughts?

hhoeflin avatar Feb 20 '20 08:02 hhoeflin

Crossref: #114

russHyde avatar Dec 09 '20 15:12 russHyde

I would still accept a PR creating such a linter, but FWIW I am increasingly against this idea.

The core of the problem is that it encourages run-time dependencies where load-time dependencies are more appropriate.

Authors insisting on :: would be well-advised to also be sure to include relevant packages in their package NAMESPACE with import() or importFrom() directives to force any .onLoad() hooks to run at load time.

But then using :: is kind of redundant since the NAMESPACE can be referenced as needed to help resolving symbols.

We recently did a sweep of {lintr}'s own codebase to remove :: usage for many highly-used calls: #2031 and #2079. A compromise was struck there where foreign calls only used a few times are kept in :: usage for emphasis/clarity.

MichaelChirico avatar Aug 11 '23 17:08 MichaelChirico