Name | flake8-mock-spec JSON |
Version |
1.4.0
JSON |
| download |
home_page | |
Summary | A linter that checks mocks are constructed with the spec argument |
upload_time | 2023-01-14 11:45:11 |
maintainer | |
docs_url | None |
author | David Andersson |
requires_python | >=3.8.1,<4.0.0 |
license | Apache 2.0 |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# flake8-mock-spec
Are you using mocks in your code and want to ensure that you are not accessing
or calling methods that the mocked objects don't have? Using mocks incorrectly
can lead to bugs in your code and falsely passing tests. To avoid this,
flake8-mock-spec linter has been created to enforce the use of the spec
argument when creating mocks. This ensures that your use of mocks is compliant
with the interface of the actual object being mocked, and helps you catch
errors early on. Using this linter can save you time and help you write more
robust and maintainable code.
## Getting Started
To start using `flake8-mock-spec`, you need to install the package and run it
on your source code. Here are the steps to get started:
1. Create a virtual environment and activate it:
```shell
python -m venv venv
source ./venv/bin/activate
```
2. Install `flake8-mock-spec`:
```shell
pip install flake8-mock-spec
```
3. Run `flake8` on your source code:
```shell
flake8 test_source.py
```
For example, consider the following code:
```Python
# test_source.py
from unittest import mock
def test_foo():
mocked_foo = mock.Mock()
```
Running `flake8` on this code will produce the following warning:
```shell
flake8 test_source.py
test_source.py:5:22: TMS010 unittest.mock.Mock instances should be constructed with the spec or spec_set argument, more information: https://github.com/jdkandersson/flake8-mock-spec#fix-tms010
```
To resolve this warning, you need to specify the `spec` or `spec_set` argument
when creating the mock object:
```Python
# test_source.py
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.Mock(spec=Foo)
```
## Rules
A set of linting rules have been defined to ensure best practices are followed
when using unittest.mock library. These rules allow for selective suppression,
meaning that specific rules can be ignored in certain scenarios. The following
rules have been defined:
* `TMS010`: checks that `unittest.mock.Mock` instances are constructed with the
`spec` or `spec_set` argument.
* `TMS011`: checks that `unittest.mock.MagicMock` instances are constructed with
the `spec` or `spec_set` argument.
* `TMS012`: checks that `unittest.mock.NonCallableMock` instances are
constructed with the `spec` or `spec_set` argument.
* `TMS013`: checks that `unittest.mock.AsyncMock` instances are constructed
with the `spec` or `spec_set` argument.
* `TMS020`: checks that `unittest.mock.patch` is called with any one or more of
the `new`, `spec`, `spec_set`, `autospec` or `new_callable` arguments
* `TMS021`: checks that `unittest.mock.patch.object` is called with any one or
more of the `new`, `spec`, `spec_set`, `autospec` or `new_callable` arguments
* `TMS022`: checks that `unittest.mock.patch.multiple` is called with any one
or more of the `spec`, `spec_set`, `autospec` or `new_callable` arguments
### Fix TMS010
This linting rule is triggered when a `unittest.mock.Mock` instance is created
without the `spec` or `spec_set` argument. For example:
```Python
from unittest import mock
def test_foo():
mocked_foo = mock.Mock()
```
To fix this issue, you need to provide the `spec` or `spec_set` argument when
creating the mock object. Here are a few examples:
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.Mock(spec=Foo)
```
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.Mock(spec_set=Foo)
```
For more information about `mock.Mock` and how to use it, please refer to the
official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock
### Fix TMS011
This linting rule is triggered when a `unittest.mock.MagicMock` instance is
created without the `spec` or `spec_set` argument. For example:
```Python
from unittest import mock
def test_foo():
mocked_foo = mock.MagicMock()
```
To fix this issue, you need to provide the `spec` or `spec_set` argument when
creating the mock object. Here are a few examples:
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.MagicMock(spec=Foo)
```
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.MagicMock(spec_set=Foo)
```
For more information about `mock.MagicMock` and how to use it, please refer to
the official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock
### Fix TMS012
This linting rule is triggered when a `unittest.mock.NonCallableMock` instance
is created without the `spec` or `spec_set` argument. For example:
```Python
from unittest import mock
def test_foo():
mocked_foo = mock.NonCallableMock()
```
To fix this issue, you need to provide the `spec` or `spec_set` argument when
creating the mock object. Here are a few examples:
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.NonCallableMock(spec=Foo)
```
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.NonCallableMock(spec_set=Foo)
```
For more information about `mock.NonCallableMock` and how to use it, please
refer to the official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.NonCallableMock
### Fix TMS013
This linting rule is triggered when a `unittest.mock.AsyncMock` instance is
created without the `spec` or `spec_set` argument. For example:
```Python
from unittest import mock
def test_foo():
mocked_foo = mock.AsyncMock()
```
To fix this issue, you need to provide the `spec` or `spec_set` argument when
creating the mock object. Here are a few examples:
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.AsyncMock(spec=Foo)
```
```Python
from unittest import mock
from foo import Foo
def test_foo():
mocked_foo = mock.AsyncMock(spec_set=Foo)
```
For more information about `mock.AsyncMock` and how to use it, please refer to the
official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.AsyncMock
### Fix TMS020
This linting rule is triggered when calling `unittest.mock.patch` without
including one or more of the following arguments: `new`, `spec`, `spec_set`,
`autospec`, or `new_callable`.
For example, this code will trigger the rule:
```Python
from unittest import mock
@mock.patch("Foo")
def test_foo():
pass
with mock.patch("Foo") as mocked_foo:
pass
foo_patcher = patch("Foo")
```
To fix this issue, include one or more of the aforementioned arguments when
calling `mock.patch`. For example:
```Python
from unittest import mock
from foo import Foo
@mock.patch("Foo", spec=Foo)
def test_foo():
pass
with mock.patch("Foo", spec_set=Foo) as mocked_foo:
pass
foo_patcher = patch("Foo", autospec=True)
```
For more information about `mock.patch` and how to use it, please refer to the
official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch
### Fix TMS021
This linting rule is triggered when calling `unittest.mock.patch.object`
without including one or more of the following arguments: `new`, `spec`,
`spec_set`, `autospec`, or `new_callable`.
For example, this code will trigger the rule:
```Python
from unittest import mock
from foo import Foo
@mock.patch.object(Foo, "bar")
def test_foo():
pass
with mock.patch.object(Foo, "bar") as mocked_foo:
pass
foo_patcher = patch(Foo, "bar")
```
To fix this issue, include one or more of the aforementioned arguments when
calling `mock.patch.object`. For example:
```Python
from unittest import mock
from foo import Foo
@mock.patch.object(Foo, "bar", spec=Foo.bar)
def test_foo():
pass
with mock.patch.object(Foo, "bar", spec_set=Foo.bar) as mocked_foo:
pass
foo_patcher = patch(Foo, "bar", autospec=True)
```
For more information about `mock.patch.object` and how to use it, please refer
to the official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch.object
### Fix TMS022
This linting rule is triggered when calling `unittest.mock.patch.multiple`
without including one or more of the following arguments: `spec`, `spec_set`,
`autospec`, or `new_callable`.
For example, this code will trigger the rule:
```Python
from unittest import mock
@mock.patch.multiple("Foo", FIRST_PATCH='bar', SECOND_PATCH='baz')
def test_foo():
pass
with mock.patch.object("Foo", FIRST_PATCH='bar', SECOND_PATCH='baz') as mocked_foo:
pass
foo_patcher = patch("Foo", FIRST_PATCH='bar', SECOND_PATCH='baz')
```
To fix this issue, include one or more of the aforementioned arguments when
calling `mock.patch.multiple`. For example:
```Python
from unittest import mock
from foo import Foo
@mock.patch.multiple("Foo", spec=Foo, FIRST_PATCH='bar', SECOND_PATCH='baz')
def test_foo():
pass
with mock.patch.object("Foo", spec_set=Foo, FIRST_PATCH='bar', SECOND_PATCH='baz') as mocked_foo:
pass
foo_patcher = patch("Foo", autospec=True, FIRST_PATCH='bar', SECOND_PATCH='baz')
```
For more information about `mock.patch.multiple` and how to use it, please
refer to the official documentation:
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch.multiple
Raw data
{
"_id": null,
"home_page": "",
"name": "flake8-mock-spec",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8.1,<4.0.0",
"maintainer_email": "",
"keywords": "",
"author": "David Andersson",
"author_email": "david@jdkandersson.com",
"download_url": "https://files.pythonhosted.org/packages/cc/6e/f43cd084997b1fe7bf6685de33ead5fdfbec84d74fb72199506baf73d985/flake8_mock_spec-1.4.0.tar.gz",
"platform": null,
"description": "# flake8-mock-spec\n\nAre you using mocks in your code and want to ensure that you are not accessing\nor calling methods that the mocked objects don't have? Using mocks incorrectly\ncan lead to bugs in your code and falsely passing tests. To avoid this,\nflake8-mock-spec linter has been created to enforce the use of the spec\nargument when creating mocks. This ensures that your use of mocks is compliant\nwith the interface of the actual object being mocked, and helps you catch\nerrors early on. Using this linter can save you time and help you write more\nrobust and maintainable code.\n\n## Getting Started\n\nTo start using `flake8-mock-spec`, you need to install the package and run it\non your source code. Here are the steps to get started:\n\n1. Create a virtual environment and activate it:\n\n ```shell\n python -m venv venv\n source ./venv/bin/activate\n ```\n\n2. Install `flake8-mock-spec`:\n\n ```shell\n pip install flake8-mock-spec\n ```\n\n3. Run `flake8` on your source code:\n\n ```shell\n flake8 test_source.py\n ```\n\nFor example, consider the following code:\n\n```Python\n# test_source.py\nfrom unittest import mock\n\ndef test_foo():\n mocked_foo = mock.Mock()\n```\n\nRunning `flake8` on this code will produce the following warning:\n\n```shell\nflake8 test_source.py\ntest_source.py:5:22: TMS010 unittest.mock.Mock instances should be constructed with the spec or spec_set argument, more information: https://github.com/jdkandersson/flake8-mock-spec#fix-tms010\n```\n\nTo resolve this warning, you need to specify the `spec` or `spec_set` argument\nwhen creating the mock object:\n\n```Python\n# test_source.py\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.Mock(spec=Foo)\n```\n\n## Rules\n\nA set of linting rules have been defined to ensure best practices are followed\nwhen using unittest.mock library. These rules allow for selective suppression,\nmeaning that specific rules can be ignored in certain scenarios. The following\nrules have been defined:\n\n* `TMS010`: checks that `unittest.mock.Mock` instances are constructed with the\n `spec` or `spec_set` argument.\n* `TMS011`: checks that `unittest.mock.MagicMock` instances are constructed with\n the `spec` or `spec_set` argument.\n* `TMS012`: checks that `unittest.mock.NonCallableMock` instances are\n constructed with the `spec` or `spec_set` argument.\n* `TMS013`: checks that `unittest.mock.AsyncMock` instances are constructed\n with the `spec` or `spec_set` argument.\n* `TMS020`: checks that `unittest.mock.patch` is called with any one or more of\n the `new`, `spec`, `spec_set`, `autospec` or `new_callable` arguments\n* `TMS021`: checks that `unittest.mock.patch.object` is called with any one or\n more of the `new`, `spec`, `spec_set`, `autospec` or `new_callable` arguments\n* `TMS022`: checks that `unittest.mock.patch.multiple` is called with any one\n or more of the `spec`, `spec_set`, `autospec` or `new_callable` arguments\n\n### Fix TMS010\n\nThis linting rule is triggered when a `unittest.mock.Mock` instance is created\nwithout the `spec` or `spec_set` argument. For example:\n\n```Python\nfrom unittest import mock\n\ndef test_foo():\n mocked_foo = mock.Mock()\n```\n\nTo fix this issue, you need to provide the `spec` or `spec_set` argument when\ncreating the mock object. Here are a few examples:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.Mock(spec=Foo)\n```\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.Mock(spec_set=Foo)\n```\n\nFor more information about `mock.Mock` and how to use it, please refer to the\nofficial documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock\n\n### Fix TMS011\n\nThis linting rule is triggered when a `unittest.mock.MagicMock` instance is\ncreated without the `spec` or `spec_set` argument. For example:\n\n```Python\nfrom unittest import mock\n\ndef test_foo():\n mocked_foo = mock.MagicMock()\n```\n\nTo fix this issue, you need to provide the `spec` or `spec_set` argument when\ncreating the mock object. Here are a few examples:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.MagicMock(spec=Foo)\n```\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.MagicMock(spec_set=Foo)\n```\n\nFor more information about `mock.MagicMock` and how to use it, please refer to\nthe official documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock\n\n### Fix TMS012\n\nThis linting rule is triggered when a `unittest.mock.NonCallableMock` instance\nis created without the `spec` or `spec_set` argument. For example:\n\n```Python\nfrom unittest import mock\n\ndef test_foo():\n mocked_foo = mock.NonCallableMock()\n```\n\nTo fix this issue, you need to provide the `spec` or `spec_set` argument when\ncreating the mock object. Here are a few examples:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.NonCallableMock(spec=Foo)\n```\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.NonCallableMock(spec_set=Foo)\n```\n\nFor more information about `mock.NonCallableMock` and how to use it, please\nrefer to the official documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.NonCallableMock\n\n### Fix TMS013\n\nThis linting rule is triggered when a `unittest.mock.AsyncMock` instance is\ncreated without the `spec` or `spec_set` argument. For example:\n\n```Python\nfrom unittest import mock\n\ndef test_foo():\n mocked_foo = mock.AsyncMock()\n```\n\nTo fix this issue, you need to provide the `spec` or `spec_set` argument when\ncreating the mock object. Here are a few examples:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.AsyncMock(spec=Foo)\n```\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\ndef test_foo():\n mocked_foo = mock.AsyncMock(spec_set=Foo)\n```\n\nFor more information about `mock.AsyncMock` and how to use it, please refer to the\nofficial documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.AsyncMock\n\n### Fix TMS020\n\nThis linting rule is triggered when calling `unittest.mock.patch` without\nincluding one or more of the following arguments: `new`, `spec`, `spec_set`,\n`autospec`, or `new_callable`.\n\nFor example, this code will trigger the rule:\n\n```Python\nfrom unittest import mock\n\n@mock.patch(\"Foo\")\ndef test_foo():\n pass\n\nwith mock.patch(\"Foo\") as mocked_foo:\n pass\n\nfoo_patcher = patch(\"Foo\")\n```\n\nTo fix this issue, include one or more of the aforementioned arguments when\ncalling `mock.patch`. For example:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\n@mock.patch(\"Foo\", spec=Foo)\ndef test_foo():\n pass\n\nwith mock.patch(\"Foo\", spec_set=Foo) as mocked_foo:\n pass\n\nfoo_patcher = patch(\"Foo\", autospec=True)\n```\n\nFor more information about `mock.patch` and how to use it, please refer to the\nofficial documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch\n\n### Fix TMS021\n\nThis linting rule is triggered when calling `unittest.mock.patch.object`\nwithout including one or more of the following arguments: `new`, `spec`,\n`spec_set`, `autospec`, or `new_callable`.\n\nFor example, this code will trigger the rule:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\n@mock.patch.object(Foo, \"bar\")\ndef test_foo():\n pass\n\nwith mock.patch.object(Foo, \"bar\") as mocked_foo:\n pass\n\nfoo_patcher = patch(Foo, \"bar\")\n```\n\nTo fix this issue, include one or more of the aforementioned arguments when\ncalling `mock.patch.object`. For example:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\n@mock.patch.object(Foo, \"bar\", spec=Foo.bar)\ndef test_foo():\n pass\n\nwith mock.patch.object(Foo, \"bar\", spec_set=Foo.bar) as mocked_foo:\n pass\n\nfoo_patcher = patch(Foo, \"bar\", autospec=True)\n```\n\nFor more information about `mock.patch.object` and how to use it, please refer\nto the official documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch.object\n\n### Fix TMS022\n\nThis linting rule is triggered when calling `unittest.mock.patch.multiple`\nwithout including one or more of the following arguments: `spec`, `spec_set`,\n`autospec`, or `new_callable`.\n\nFor example, this code will trigger the rule:\n\n```Python\nfrom unittest import mock\n\n@mock.patch.multiple(\"Foo\", FIRST_PATCH='bar', SECOND_PATCH='baz')\ndef test_foo():\n pass\n\nwith mock.patch.object(\"Foo\", FIRST_PATCH='bar', SECOND_PATCH='baz') as mocked_foo:\n pass\n\nfoo_patcher = patch(\"Foo\", FIRST_PATCH='bar', SECOND_PATCH='baz')\n```\n\nTo fix this issue, include one or more of the aforementioned arguments when\ncalling `mock.patch.multiple`. For example:\n\n```Python\nfrom unittest import mock\n\nfrom foo import Foo\n\n@mock.patch.multiple(\"Foo\", spec=Foo, FIRST_PATCH='bar', SECOND_PATCH='baz')\ndef test_foo():\n pass\n\nwith mock.patch.object(\"Foo\", spec_set=Foo, FIRST_PATCH='bar', SECOND_PATCH='baz') as mocked_foo:\n pass\n\nfoo_patcher = patch(\"Foo\", autospec=True, FIRST_PATCH='bar', SECOND_PATCH='baz')\n```\n\nFor more information about `mock.patch.multiple` and how to use it, please\nrefer to the official documentation:\nhttps://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch.multiple\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "A linter that checks mocks are constructed with the spec argument",
"version": "1.4.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3680531471bbe770cf01123ffa0cc74c4af2aa7172902a5c219f7a79fe0b1bf6",
"md5": "e6fbd6e184076a687ed8aaec7d1e09c2",
"sha256": "9e003d03e5aec650445f3d440aba5c8c41898a2ca45249c0019cef2c11650725"
},
"downloads": -1,
"filename": "flake8_mock_spec-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e6fbd6e184076a687ed8aaec7d1e09c2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8.1,<4.0.0",
"size": 9391,
"upload_time": "2023-01-14T11:45:10",
"upload_time_iso_8601": "2023-01-14T11:45:10.235048Z",
"url": "https://files.pythonhosted.org/packages/36/80/531471bbe770cf01123ffa0cc74c4af2aa7172902a5c219f7a79fe0b1bf6/flake8_mock_spec-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cc6ef43cd084997b1fe7bf6685de33ead5fdfbec84d74fb72199506baf73d985",
"md5": "16d967988d5a267bddf5b8856112a44b",
"sha256": "6f7f18a2f85275b7eb6d17565dc56d41a1330f910c4288667980665a7a787759"
},
"downloads": -1,
"filename": "flake8_mock_spec-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "16d967988d5a267bddf5b8856112a44b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8.1,<4.0.0",
"size": 10166,
"upload_time": "2023-01-14T11:45:11",
"upload_time_iso_8601": "2023-01-14T11:45:11.512421Z",
"url": "https://files.pythonhosted.org/packages/cc/6e/f43cd084997b1fe7bf6685de33ead5fdfbec84d74fb72199506baf73d985/flake8_mock_spec-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-14 11:45:11",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "flake8-mock-spec"
}