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
Missing Type hints and VSCode autocompletion #1893
Comments
This seems to be the same as in #1604. |
I've red the issue you've linked and it might be related indeed. I'm just not sure if the "tab completion" in REPL is the same as for VSCode Python extension. I think the former is using the The later is more based (AFAIK) on type annotations on the code. The extension is doing a static analysis of an object based on his type to collect possible attributes or methods. In any cases this is not working and it makes it tedious to use faker |
I think we've already done everything we could do, short of rewriting But I'm open to ideas! If anybody knows how to help, please submit a Pull Request! |
Thé issue is that Faker instances are mostly proxies around providers. This dynamic behavior cause static type checking tools to not be able to resolve types because they’re not aware of those proxied providers. The only solution I could see is that we generate type stubs from the providers and « attach them » to the faker object. Those stubs .pyi files can then be shipped next to faker package. |
In the meanwhile, I found the following workaround. It is a bit of a hack to trick the type checker from typing import cast
from faker import Faker, Generator
from faker.providers import address, date_time, python
class SmartFaker(
date_time.Provider,
address.Provider,
python.Provider,
Faker,
Generator,
):
unique: "SmartFaker"
optional: "SmartFaker"
def __init__(self, generator: Any) -> None:
msg = "This class is meant to be used only as a type"
raise RuntimeError(msg)
def create_faker() -> SmartFaker:
return cast(SmartFaker, Faker()) The idea is to create a "fake" class inheriting all the providers you want plus Faker and Generator. Then you use this class just as a type by either casting, using a factory function or typing a "fake" object in a function arg to get full type checking + intelisense.
Well, this is a bit ugly but it actually saves me lot of time going back and forth the online documentation to check available providers and parameters. |
@g0di I like your workaround! For me, vscode is reporting a conflict with the Faker class and Generator class though, on the |
I don’t remember having such an issue. Maybe because I disabled pyright and I’m using mypy for type checking? I’ll have to check that on Monday. Anyway as I mentioned, this is just a trick, feel free to edit to make it for your case ;) |
This issue is stale because it has been open for 30 days with no activity. |
This issue was closed because it has been inactive for 14 days since being marked as stale. |
Using Faker is a bit tedious to me at the moment because I have no type hints through my VSCode editor. I must regularly go to the documentation and check in all providers to find the function that would suit me the most compared to having function suggested directly by my editor when I type
faker.
.Steps to reproduce
Expected behavior
I expect that VSCode provides autocompletions for available providers functions (like
.name()
)Actual behavior
VSCode mark the
fake.name()
function asAny
and does not provide any autocompletionsThe text was updated successfully, but these errors were encountered: