faker: Tab completion in interactive shell not working since Python version 3.8

Tab autocomplete in python interactive shell unavailable when using Python versions 3.9, 3.10 or 3.11.

Python documents this feature: https://docs.python.org/3.11/tutorial/interactive.html#tab-completion-and-history-editing. (Docs here are the same in all versions of Python)

Setup

  • Faker version: 17.3.0
  • OS: Debian GNU/Linux 11 (bullseye)
OS versions
PS C:\Users\User> docker run -it --rm python:3.8 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

PS C:\Users\User> docker run -it --rm python:3.9 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

PS C:\Users\User> docker run -it --rm python:3.10 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

PS C:\Users\User> docker run -it --rm python:3.11 cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

What used to work

# pip install faker=="17.3.0"
# python
>>> import faker
>>> fake = faker.Faker()
>>> fake.

Type [TAB] once or twice and you see this:

Display all 280 possibilities? (y or n)
fake.aba(                             fake.generator_attrs                  fake.pyfloat(
fake.add_provider(                    fake.get_arguments(                   fake.pyint(
fake.address(                         fake.get_formatter(                   fake.pyiterable(
... I've removed a whole bunch for brevity's sake.
fake.free_email(                      fake.psv(                             fake.zipcode_in_state(
fake.free_email_domain(               fake.pybool(                          fake.zipcode_plus4(
fake.future_date(                     fake.pydecimal(
fake.future_datetime(                 fake.pydict(
>>> fake.

What’s broken

In Python 3.9, 3.10, and 3.11:

# pip install faker=="17.3.0"
# python
>>> import faker
>>> fake = faker.Faker()
>>> fake.

Type [TAB] once or twice and nothing happens.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 10
  • Comments: 50 (23 by maintainers)

Most upvoted comments

I’m just commenting to say that I’d greatly appreciate this feature as well. I’d imagine it would be trivial to add a mypy stub or similar to declare all of the many provider’s attributes as members of the base Faker class. I’m trying to write the stub file myself, I’ll try to remember to report back if it works.

This is a night and day difference.

Environment

Instructions

mkdir -p ~/projects/python; cd ~/projects/python
git clone https://github.com/joke2k/faker
git clone https://github.com/KaylaHood/faker-stubs

cd ~/projects/python/faker
virtualenv .venv
. .venv/bin/activate
pip install -e .
python ../faker-stubs/generate_stub.py

You should now see this with git status:

❯ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        faker/proxy.pyi

If the faker/proxy.pyi stub is there, you will be able to see typings if you open any file, e.g. tests/test_proxy.py

Before (via here)

from faker import Faker

def test_faker(faker: Faker):
    assert isinstance(faker.name(), str)

image

With faker-stubs @ 77d25e7

image

image

Locale / faker constructor

re: locales

from faker import Faker

fake = Faker(locale="en")

Expected no arguments to “Faker” constructor (Pyright reportGeneralTypeIssues)

If the constructor itself allowed locale, we’d get around this particular one. Though we wouldn’t have locale-specific ones.

image

I see the same problem on Python 3.9 in PyCharm. I wonder if this is just due to the high level of dynamism in Faker? I.e. attributes are added in via __getattribute__ so static type checkers cannot resolve them?

But makes the library a pain to use as not discoverable via IDE.

image These are the attributes I see on faker instance.

Can anyone else replicate this?

I showed you how to run this in docker, so hopefully it should be easy enough to duplicate. (The issue can be demonstrated outside of docker btw)

@tony I assume that pinging @fcurella here would make much sense, as he is the active maintainer at the moment.

@tony well I seem to have made something helpful but objectively not very Pythonic. https://github.com/KaylaHood/faker-stubs

I wrote a little script that makes heavy use of the standard lib inspect package to generate a stub for Faker’s proxy.py file so that auto-completion for faker.Faker() instances will contain every provider method for every locale. It works for my use case (Windows 10, Python 3.9), but I didn’t spend time testing it for other platforms.

You have to run the stub generator every time you install/upgrade Faker; I can see it getting tedious very quickly.
If you use poetry or some other python package management tool you could write a post-build script that automates the download and execution of my stub generation script.

NOTE: the automated stub generation solution I just described should NEVER be used for a production or client-facing application. You don’t know me! I could change my stub generation script to be a data miner! (I wouldn’t, but y’know what I mean) If you would like to incorporate it into your product, then consider forking my repo to your own github/artifactory/server/etc and then use your cloned version of my script in your product’s build pipeline.

Hopefully you find it useful!

Summary

  1. IntelliCode and Kite-AI Auto-completion not working in VSCode.

    Screenshot (Click to expand)
  2. Python Docs unavailable both in VSCode IntelliCode and Kite Copilot.

    Screenshots (Click to expand)
  3. Auto-completion available as it should in IPython, however …

    Screenshot

Environments

  • Windows 10 Professional 21H2 (19044.1566)
  • Miniconda3, installed via winget.exe
    • conda 4.11.0
    • virutalenv 20.4.6
    • Python 3.9.7
      • Faker 13.0.0
      • IPython 8.1.0
  • Microsoft Visual Studio Code 1.65.0 (system setup)
  • Kite Engine 1.2021.610.0
    • VSCode Plugin Installed
    • Vim Plugin Installed

Steps to reproduce

  1. Setup Python 3.9 or later version

    winget install Python.Python.3
    
  2. Install the latest version of Faker

  3. Installed the latest version of Microsoft Visual Studio Code

    winget install Microsoft.VisualStudioCode
    
  4. Open an empty folder as a VSCode workspace, and create an empty *.py file

  5. Type in the following snippet

    from faker import Faker
    
    fake = Faker(locale='en_US')
    fake.
    
  6. Invoke auto-completion (mostly Ctrl + Space) after the dot, check if all available methods are listed.

Expected Behavior

  1. All available methods should be listed, as it’s shown in IPython Console.
  2. Python Docs are automatically fetched from local (or remote) when cursor on any one of Faker’s class, instance or method.

Actual Behavior

As I mentioned before in “Summary” section.

Notes

Kite Engine and IntelliCode for VSCode are enabled and ready. It’s not Kite or VSCode’s fault.

Screenshot (Click to expand)

Of course! I’d love that!

On Tue, Oct 17, 2023 at 2:16 PM Viicos @.***> wrote:

@fcurella https://github.com/fcurella Would you be open to have a PR implementing the generation of a stub file like @KaylaHood https://github.com/KaylaHood did? This would generate a single proxy.pyi file that would be shipped in next versions. We could make sure the file is generated again each time a provider implements a new method.

If so, I can open a PR with a script inspirede by @KaylaHood https://github.com/KaylaHood’s work

— Reply to this email directly, view it on GitHub https://github.com/joke2k/faker/issues/1604#issuecomment-1767017302, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAV4B37DSY7IZZ5DCEIST3X73KRPAVCNFSM5NP7YUK2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZWG4YDCNZTGAZA . You are receiving this because you were mentioned.Message ID: @.***>

Me too! It is a big caveat for me, that there is no any type hints for possible data. Mimesis offers that, so if there is comparison, lack of annotations is much bigger deal

@KaylaHood Follow up to https://github.com/joke2k/faker/issues/1604#issuecomment-1312496383 + https://github.com/joke2k/faker/issues/1604#issuecomment-1312799159

Environment

New docstring completions + locale kwarg

This looks good.

I had forgotten to include the mangled methods from the Faker class (__init__, __deepcopy__, etc). Now the __init__ method’s signature will include the optional locale argument (as well as the other optional args).

Got it and this looks good (screenshot 1)

I fixed the docstrings in the stubs so now the docstrings will populate in the auto-completion results.

Roger that, and this looks good (screenshot 1)

image Above: Screenshot 1

Provider functions v. member names

I also realized certain provider functions have the same name as some member variables (example: country_code). I added logic that removes those conflicting member variables from the stub in favor of the provider function.

Good catch here as well

Before, with faker-stubs @ 77d25e7

image

With faker-stubs @ d555b36:

image

With faker-stubs @ 5161681:

image

With faker-stubs @ 5c7ffc2:

image

Side note, re: **config: Any

**config: Any on the faker side permits anything, https://github.com/joke2k/faker/blob/bc56a8f37f4a1aef3b2fc6c4efb69cfcf28606e0/faker/proxy.py#L34

I’ve never used any **config kwargs, so I’m not sure what this is used for. If it’s something deterministic, there’s new PEPs and backported support available:

@KaylaHood I try your repo and it works great on my device! Thank you!

Environments

  • Microsoft Windows 11 Professional 22H2
  • Ubuntu 22.04 LTS on WSL2
  • Microsoft Visual Studio Code v1.73.1 (system setup)
  • Python v3.10.7
  • Poetry v1.2.2
  • Faker v15.3.1

Screenshots

image

We could integrate @KaylaHood 's script in our CI build and run it before every release, but that would still not cover community providers that users have installed on their own.

On one hand, that’d be better than nothing. On the other, it may set the wrong expectation on users.

@KaylaHood You are correct in factories and metaclasses masking completions due to being reliant on runtime.

If you accomplish anything it’d be nice to see where it goes. But it may be too runtime-heavy to get completion and typings for.

Everybody wants completions for faker

edit: I don’t believe anybody specifically doesn’t want it 😃

In Ubuntu 20.04 I can’t replicate it with Python3.9 installed using APT from default repositories, but Python3.10 installed from deadsnakes PPA has this problem.