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

add type annotations #152

Closed
chdsbd opened this issue Jul 28, 2019 · 9 comments · Fixed by #199
Closed

add type annotations #152

chdsbd opened this issue Jul 28, 2019 · 9 comments · Fixed by #199

Comments

@chdsbd
Copy link

chdsbd commented Jul 28, 2019

I think it may be helpful to add type annotations to this package.

@nicoddemus
Copy link
Member

Thanks for the suggestion @chdsbd. I think this would be best when pytest-mock supports Python 3 only so we can use native type annotations.

@JakeSummers
Copy link

JakeSummers commented Apr 2, 2020

There are some reasonable workarounds to this problem.

Right now I have a test like this:

def test_car(mocker: MockFixture):
    mocker.patch.object(Engine, "run")
    car = Car()
    car.start()
    Engine.run.assert_any_call(... )

When I run it through mypy, I get the following error:

test_car.py:30: error: "Callable[[Engine, ...], ...]" has no attribute "assert_any_call"

Workaround #1 - Ignore the error

def test_car(mocker: MockFixture):
    mocker.patch.object(Engine, "run")
    car = Car()
    car.start()
    Engine.run.assert_any_call(... ) # ignore: type

Workaround #2 - Use the result of the patch command.

def test_car(mocker: MockFixture):
    engine_mock:MagicMock = mocker.patch.object(Engine, "run")
    car = Car()
    car.start()
    engine_mock.assert_any_call(... ) # ignore: type

@srittau
Copy link
Contributor

srittau commented Jun 26, 2020

FWIW, I have just opened python/typeshed#4275. This will add stubs to typeshed for now. Of course, I'd be glad to see the stubs in pytest-mock directly some day, either via separate stub files or inline.

@bluetech
Copy link
Member

@srittau What gets priority when a package has its own stubs, and is also defined in typeshed?

@JelleZijlstra
Copy link

@bluetech
Copy link
Member

Thanks @JelleZijlstra. So it should be fine if we ever decide to ship type annotations inline.

@srittau
Copy link
Contributor

srittau commented Jun 28, 2020

As soon as that happens, we should remove the stubs from typeshed anyway, to avoid confusion and reduce typeshed's maintenance burden.

@jtpavlock
Copy link

Pytest 6.0 now supports type annotations and pytest-mock is now python3.5+ 👀

@nicoddemus
Copy link
Member

In #194 we now run mypy without errors on pytest-mock itself (thanks to @staticdev), so at least that step is done.

The next step would probably be to type annotate the public API and export a py.typed file along the package.

sbdchd added a commit to chdsbd/kodiak that referenced this issue Aug 11, 2020
Now we explicitly ignore specific imports instead of a blanket ignore
on all missing types.

Upgraded databases and pytest to get their latest versions which include
types, for the rest we're ignoring them explicitly.
see #485

- arrow

  doesn't have types but we can replace it with std library calls

- requests_async

  deprecated in favor of httpx, so we'd need to upgrade to get types.
  The API of httpx is a little different as well.

- structlog

  doesn't have types currently

  hynek/structlog#165

- rure

  No stubs yet

  davidblewett/rure-python#23

- pytest_mock

  types recently added to the typeshed so we'll need to wait for the next
  release of mypy.

  pytest-dev/pytest-mock#152

- markdown_html_finder

  Wrapper around some Rust code. Needs stubs to be added.

  chdsbd/markdown-html-finder#5

- asyncio_redis

  We're our own fork of it, but adding types would be rather manual.
  Granted we aren't using that much of the API surface.

  https://github.com/chdsbd/asyncio-redis

- zstandard

  No stubs yet, so we're ignoring for the time being

  indygreg/python-zstandard#120

- inflection

  Types, but I think they're misconfigured, once they're fixed we can
  unignore.

  jpvanhal/inflection#49

- django

  Need to setup django-stubs for this to work since django doesn't have
  any static types.

  https://github.com/TypedDjango/django-stubs

- dj_database_url

  Doesn't have any static types yet

  jazzband/dj-database-url#135

- responses

  No types yet

  getsentry/responses#339

- stripe

  Plan for types, but they don't exist currently

  stripe/stripe-python#650
kodiakhq bot pushed a commit to chdsbd/kodiak that referenced this issue Aug 11, 2020
Now we explicitly ignore specific imports instead of a blanket ignore
on all missing types.

Upgraded databases and pytest to get their latest versions which include
types, for the rest we're ignoring them explicitly.
see #485

- arrow

  doesn't have types but we can replace it with std library calls

- requests_async

  deprecated in favor of httpx, so we'd need to upgrade to get types.
  The API of httpx is a little different as well.

- structlog

  doesn't have types currently

  hynek/structlog#165

- rure

  No stubs yet

  davidblewett/rure-python#23

- pytest_mock

  types recently added to the typeshed so we'll need to wait for the next
  release of mypy.

  pytest-dev/pytest-mock#152

- markdown_html_finder

  Wrapper around some Rust code. Needs stubs to be added.

  chdsbd/markdown-html-finder#5

- asyncio_redis

  We're using our own fork of it, but adding types would be rather manual.
  Granted we aren't using that much of the API surface.

  https://github.com/chdsbd/asyncio-redis

- zstandard

  No stubs yet, so we're ignoring for the time being

  indygreg/python-zstandard#120

- inflection

  Types, but I think they're misconfigured, once they're fixed we can
  unignore.

  jpvanhal/inflection#49

- django

  Need to setup django-stubs for this to work since django doesn't have
  any static types.

  https://github.com/TypedDjango/django-stubs

- dj_database_url

  Doesn't have any static types yet

  jazzband/dj-database-url#135

- responses

  No types yet

  getsentry/responses#339

- stripe

  Plan for types, but they don't exist currently

  stripe/stripe-python#650
nicoddemus added a commit that referenced this issue Aug 22, 2020
Fix #152

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
srittau added a commit to srittau/typeshed that referenced this issue Aug 24, 2020
pytest-mock is now type annotated, which makes typeshed's annotations
obsolete. See pytest-dev/pytest-mock#152.
hauntsaninja pushed a commit to python/typeshed that referenced this issue Aug 29, 2020
pytest-mock is now type annotated, which makes typeshed's annotations
obsolete. See pytest-dev/pytest-mock#152.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants