Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cannot find reference 'v4' in '__init__.py'? :/#2807

Answeredbynejch
stdedos asked this question inQ&A
Discussion options

I'm trying to add some type hints to my functions, e.g.project: gitlab.v4.objects.Project.

However, I am getting this pesky warning from IDEA:
image

Am I doing something wrong? 😓 What am I missing?

You must be logged in to vote
Answered by nejchFeb 22, 2024

This would be perfectly fine for mypy. I guess you're hitting a pycharm/IDEA bug where it expects that package exposed via__all__:

https://stackoverflow.com/questions/23248017/cannot-find-reference-xxx-in-init-py

You can probably work around it by importing it directly. Dummy code:

fromgitlab.v4.objectsimportProjectdefget_project(project:Project)->Nonepass

Replies: 1 comment 4 replies

Comment options

This would be perfectly fine for mypy. I guess you're hitting a pycharm/IDEA bug where it expects that package exposed via__all__:

https://stackoverflow.com/questions/23248017/cannot-find-reference-xxx-in-init-py

You can probably work around it by importing it directly. Dummy code:

fromgitlab.v4.objectsimportProjectdefget_project(project:Project)->Nonepass
You must be logged in to vote
4 replies
@stdedos
Comment options

I wanted to avoidfrom gitlab.v4.objects import Project, because then, I had toimport everything:

Project, jobs.ProjectJob, ProjectMergeRequest, etc

And doingfrom gitlab.v4 import objects sounds ... atrocious (seeing it randomly in the code, would make me ask "object.? What "object"??"

But I solved my problem withfrom gitlab.v4 import objects as gobjects

Thank you for the suggestion / digging 🙃

@nejch
Comment options

Glad you got it to work! Yes, the exact level at which you import is up to you, as long as you trick your IDE's type checker around the__all__ bug. 🙂

@stdedos
Comment options

... truth be told though, it's impossible to write:

$ipythonPython3.9.18 (main,Aug252023,13:20:04)Type'copyright','credits'or'license'formoreinformationIPython8.14.0--AnenhancedInteractivePython.Type'?'forhelp.***`ipython-autoimport`notfound***In [1]:importgitlab.v4.objects.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[1],line1---->1importgitlab.v4.objects.ProjectModuleNotFoundError:Nomodulenamed'gitlab.v4.objects.Project'In [2]:importgitlab.v4.objects.projects.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[2],line1---->1importgitlab.v4.objects.projects.ProjectModuleNotFoundError:Nomodulenamed'gitlab.v4.objects.projects.Project';'gitlab.v4.objects.projects'isnotapackageIn [3]:importgitlab.v4.objects.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[3],line1---->1importgitlab.v4.objects.ProjectModuleNotFoundError:Nomodulenamed'gitlab.v4.objects.Project'In [4]:importgitlab.objects.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[4],line1---->1importgitlab.objects.ProjectModuleNotFoundError:Nomodulenamed'gitlab.objects'In [5]:importgitlab.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[5],line1---->1importgitlab.ProjectModuleNotFoundError:Nomodulenamed'gitlab.Project'In [6]:importgitlabIn [7]:importgitlab.v4.objects.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[7],line1---->1importgitlab.v4.objects.ProjectModuleNotFoundError:Nomodulenamed'gitlab.v4.objects.Project'In [8]:importgitlab.v4.objects.projects.CellIn[8],line1importgitlab.v4.objects.projects.^SyntaxError:invalidsyntaxIn [9]:importgitlab.v4.objects.projectsIn [10]:importgitlab.v4.objects.projects.Project---------------------------------------------------------------------------ModuleNotFoundErrorTraceback (mostrecentcalllast)CellIn[10],line1---->1importgitlab.v4.objects.projects.ProjectModuleNotFoundError:Nomodulenamed'gitlab.v4.objects.projects.Project';'gitlab.v4.objects.projects'isnotapackageIn [11]:

I am very much missing the point in Python imports, namespaces, and packaging though, so - I could very well be wrong.

@nejch
Comment options

nejchMar 4, 2024
Maintainer

You might be mixing up class import (from abc import xyz) with module/package imports (import abc). It's up to you how you want to import them but you should use the right way to access them then - don't try to import a class without afrom syntax.

>>>importgitlab.v4.objects>>>gitlab.v4.objects.Project<class'gitlab.v4.objects.projects.Project'>>>>importgitlab.v4.objects.projects>>>gitlab.v4.objects.projects.Project<class'gitlab.v4.objects.projects.Project'>>>>fromgitlab.v4.objectsimportProject>>>Project<class'gitlab.v4.objects.projects.Project'>
Answer selected bystdedos
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@stdedos@nejch

[8]ページ先頭

©2009-2025 Movatter.jp