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

Merge type hints to Python files from my typings folder? #1788

Open
zarif98 opened this issue Sep 28, 2024 · 7 comments
Open

Merge type hints to Python files from my typings folder? #1788

zarif98 opened this issue Sep 28, 2024 · 7 comments

Comments

@zarif98
Copy link

zarif98 commented Sep 28, 2024

I have a bunch of stub files organized by my C# stub generator DLLs, which are organized by DLL namespace names. They are then called by my Python scripts using clr.addReference("name"), which is a DLL name called name.dll, and then I import the classes that I need to run my scripts like from "name" import "hello". MyPy and VsCode is able to recognize these stubs and is able to proper syntax highlighting. How would I go about merging those types with my Python code?

It seems like merge-pyi tool wants the stub to have the same name as the python script? Is there a way I can work around and load all of my stubs and match it with my Python scripts the same way VsCode and MyPy does it?

@h-joo
Copy link
Contributor

h-joo commented Sep 30, 2024

I think I didn't fully understand your use case, but in merge_pyi there is a flag to indicate which Python file, stub file to use, were these not usable?

@zarif98
Copy link
Author

zarif98 commented Sep 30, 2024

So my python stubs are for the modules they are calling.

Like for example, my Python code looks like this:

from Microsoft-Stubs.CodeAnalysis import HelloWorld()

This is my generated Python stub tree structure:
image
They all have and init.pyi file within the folders as well

I am using this GitHub repo to generate my Python stubs: https://github.com/Mimer29or40/pythonnet-stubs

And Visual Studio Code and MyPy are able to load the stubs appropriately and even let me go into the stubs themselves giving me auto-complete and syntax highlighting. Is it possible to have it how Visual Studio Code and MyPy are able to do automatically? I have way too many stubs to parse through since it is our entire DLL codebase written in Python stubs.

@h-joo
Copy link
Contributor

h-joo commented Sep 30, 2024

sadly, the answer would be no, and I don't think we have resources right now to support such feature 😓

@zarif98
Copy link
Author

zarif98 commented Sep 30, 2024

Could we label this is a future feature then? Would be really nice to have in the future since my modules are organized nearly enough for Pylance, Python and MyPy to recognize

@martindemello
Copy link
Contributor

it's worth experimenting with generating an imports_info file corresponding to your module structure and passing that to pytype. might need a bit of work to support passing the imports info to pytype and having it pass that on to pytpye-single.

@zarif98
Copy link
Author

zarif98 commented Sep 30, 2024

Any ideas on how to generate one? Sorry, what is an imports_info file? I can try experimenting today.

@martindemello
Copy link
Contributor

sorry, my mistake, i forgot merge_pyi does not use an imports_info file. if you would like to try adding the feature, the key line of code to change is here: https://github.com/google/pytype/blob/main/pytype/tools/merge_pyi/merge_pyi.py#L146

the rough outline of the change is:

  1. change merge_project to take an optional imports_map argument (here: https://github.com/google/pytype/blob/main/pytype/tools/merge_pyi/merge_project.py#L10)
  2. deserialise the imports_map file into a python dict and pass it to merge_tree here: https://github.com/google/pytype/blob/main/pytype/tools/merge_pyi/merge_project.py#L70
  3. in merge_tree, change this bit of code to if imports_map: pyi_file = imports_map[f] else: pyi = path_utils.join(pyi_dir, f + "i")

in your own project you need to generate the imports_map dictionary as a map of {python_file: pyi file} and serialise it in a way that merge_project can read back (i suggest json)

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

3 participants