docs icon indicating copy to clipboard operation
docs copied to clipboard

[bug] conan.tools.scm.Version cannot be used from conan.tools import

Open danimtb opened this issue 3 years ago • 4 comments

Happens at least in conan 1.51.3 / 1.51.2 / 1.51.1 / 1.50.2 / 1.50.1 / 1.50.0

(conan) λ python                                                                                 
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information.                           
>>> from conan import tools                                                                      
>>> tools.scm.Version("1.2.1")                                                                   
Traceback (most recent call last):                                                               
  File "<stdin>", line 1, in <module>                                                            
AttributeError: module 'conan.tools' has no attribute 'scm'

The workaround is easy as importing like this works fine:

from conan.tools import scm
>>> scm.Version("1.2.1")                       
1.2.1
>>> from conan.tools.scm import Version                                                          
>>> Version("1.2.1")                                                                             
1.2.1                                                                                            

However the tools.scm.Version should be possible for anyone to use it

danimtb avatar Aug 24 '22 11:08 danimtb

This is because tools is a package, not a module, and it doesn't automatically export all of its subpackages. It is not designed to be imported and used that way (if it is documented to be used that way, we should probably fix the docs), precisely to avoid importing a ton of tools that are not going to be used (that also increments Conan command startup time). Tools imports should be imported like from conan.tools.xxxx import yyyy

memsharded avatar Aug 24 '22 11:08 memsharded

from conan import tools works in other cases. See for example https://github.com/conan-io/conan-center-index/pull/12067

The only difference I see is that Version is a class instead of a function and it could be more difficult to use it from tools directly.

It is not a big deal, but there might be issues in the future as the import cannot be limited in the way the user does it and having this error is confusing (the user may think class Version does not exist or that the documentation is wrong or that the conan version is outdated)

danimtb avatar Aug 24 '22 11:08 danimtb

It might eventually work for some things, not sure if the class/function is the difference, but it is not documented usage. In your PR to conan-center-index, it works by luck, but if you replicate your above, it will fail:

>>> from conan import tools
>>> tools.files.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'conan.tools' has no attribute 'files'

In that PR it works, because some other internal imports from from tools.cmake... are internally importing from tools.files and bringing that into the namespace, but otherwise it would also fail.

The documented usages are described in https://docs.conan.io/en/latest/reference/conanfile/tools.html, and explicitly says that they should be imported from conan.tools. All examples in the docs are written in that way too. It is also more pythonic, like importing from collections import OrderedDict instead of importing collections then collections.OrderedDict. And I would say even more for multiple nested level of subpackages, it doesn't sound like Python best practice.

memsharded avatar Aug 24 '22 12:08 memsharded

Moving this to the docs, to explicitly clarify this.

memsharded avatar Aug 24 '22 12:08 memsharded