Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional imports and reportMissingImports #709

Closed
bluss opened this issue Sep 28, 2024 · 2 comments
Closed

Conditional imports and reportMissingImports #709

bluss opened this issue Sep 28, 2024 · 2 comments

Comments

@bluss
Copy link

bluss commented Sep 28, 2024

How to deal with conditional imports in a good way. This is a pretty common one: use tomllib when it's available (Python 3.11+) else depend on tomli.

basedpyright is configured to check Python 3.8+ compatibility but the development venv is installed with python 3.12. This seems to create a conflict (?), for example that tomli is required for Py 3.8 but it's not installed because the development environment doesn't need it. There is no venv configuration that does not cause unecessary

My best guesses from the pyright playground is that this issue exists there too.
I don't know what a solution looks like here. See the diagnostics below for a description of what happens.

Project dependencies:

dependencies = [
    "tomli>=2.0.1; python_version < '3.11'",
]

Configuration:

[tool.pyright]
pythonVersion = "3.8"
include = ["src"]
extraPaths = ["src"]
venvPath = "."
venv = ".venv"
exclude = [".venv"]
typeCheckingMode = "basic"

Development environment installation:

  • venv python: 3.12. tomli not installed.
  • (It's also possible to use: venv python: 3.8: tomli installed, tomllib not available.)

First version of the code:

try:
    import tomllib as tomli
except ImportError:
    import tomli as tomli
  try:                                                                                                                                                           
*     import tomllib as tomli                                                                                                                                    
             └──── reportMissingImports: Import "tomllib" could not be resolved                                                                                  
  except ImportError:                                                                                                                                            
*     import tomli as tomli                                                                                                                                      
             └──── reportMissingImports: Import "tomli" could not be resolved

Alternative version of the code:

if sys.version_info >= (3, 11):
    import tomllib as tomli
else:
    import tomli as tomli
  if sys.version_info >= (3, 11):                                                                                                                                
      import tomllib as tomli                                                                                                                                    
      └──── reportUnreachable: Code is unreachable                                                                                                               
  else:                                                                                                                                                          
*     import tomli as tomli                                                                                                                                      
             └──── reportMissingImports: Import "tomli" could not be resolved
basedpyright --version
basedpyright 1.17.5
based on pyright 1.1.381
@DetachHead
Copy link
Owner

merging with #8 as i believe that would solve this issue

@DetachHead DetachHead closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2024
@bluss
Copy link
Author

bluss commented Sep 29, 2024

Thanks for the work on basedpyright!

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

No branches or pull requests

2 participants