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

Go to definition by python module path in string #3700

Closed
diego351 opened this issue Dec 1, 2022 · 10 comments
Closed

Go to definition by python module path in string #3700

diego351 opened this issue Dec 1, 2022 · 10 comments
Assignees
Labels
django Related to django support enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@diego351
Copy link

diego351 commented Dec 1, 2022

MacOS 13.0
VSCode Version: 1.73.1
Pylance v2022.11.30
Pylance language server 2022.11.30

Django heavily uses python module paths in strings for configuration.

settings.py

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

Another example:

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
]

urls.py

 url(r"^transmissions", include("transmissions.urls")),

I would like to have option to go to the definition by these strings

@rchiodo
Copy link
Contributor

rchiodo commented Dec 1, 2022

Can you give an example on where you'd go to definition? Do you mean where you're using say INSTALLED_APPS[3]?

@diego351
Copy link
Author

diego351 commented Dec 1, 2022

@rchiodo I agree that while classes paths are fairly obvious in terms of behaviour, for non project directories (majority of cases) it's more complicated. Ideal world scenario would be showing the library directory somehow in the file explorer, perhaps as separate folder in workspace?

@rchiodo
Copy link
Contributor

rchiodo commented Dec 1, 2022

@djego351 I'm sorry but I don't think I understand what it is you're asking for. Goto definition on what exactly?

@rchiodo rchiodo added the django Related to django support label Dec 1, 2022
@diego351
Copy link
Author

diego351 commented Dec 1, 2022

@rchiodo Sorry, when I go to definition of "django.middleware.security.SecurityMiddleware" I want to go to definition of SecurityMiddleware sitting in site-packages/django/middleware/security.py. For going to the definition of package (directory) in example django app "django.contrib.admin", the best scenario would be showing the package directory in the file explorer.

I hope that makes sense

@rchiodo
Copy link
Contributor

rchiodo commented Dec 1, 2022

Ah okay, so you want special functionality for strings in general or is there some other way for us to identify these are actually package names?

@diego351
Copy link
Author

diego351 commented Dec 1, 2022

I'm thinking about it now. Not really much more than optionally dot separated words. Eh, that might be dirty.

@rchiodo
Copy link
Contributor

rchiodo commented Dec 1, 2022

Are these strings always in a settings.py file? Maybe we could use that to filter it down?

Strings in settings.py that contain '.' in them and no spaces or other punctuation

@diego351
Copy link
Author

diego351 commented Dec 2, 2022

@rchiodo Not always in settings.py unfortunately. It's also in urls.py and models.py.

urls.py

 url(r"^transmissions", include("transmissions.urls")),

Here in slightly different way: (would normally be "transmissions.models.Sport" but django ommits that in ForeignKey, OneToOneField, ManyToManyField and possibly more)
models.py

sport = models.ForeignKey("transmissions.Sport")

In general it can be seen in many places so I would treat that as a django thing, but also other libraries use that too.

The general question, how much appreciated is making annotations to competition's solution?

@judej judej added the enhancement New feature or request label Dec 6, 2022
@rchiodo rchiodo self-assigned this Jan 6, 2023
@PylanceBot PylanceBot added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Apr 25, 2023
@rchiodo
Copy link
Contributor

rchiodo commented Apr 26, 2023

This issue has been fixed in prerelease version 2023.4.41, which we've just released. You can find the changelog here: CHANGELOG.md

It has a setting that must be set to true for it to work though:

  "python.analysis.gotoDefinitionInStringLiteral": true,

@diego351
Copy link
Author

diego351 commented May 6, 2023

Works beautifully! Thanks you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
django Related to django support enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants