Skip to content

Commit

Permalink
replace mock with unittest.mock (#25774)
Browse files Browse the repository at this point in the history
> mock is now part of the Python standard library, available as
unittest.mock in Python 3.3 onwards.

## How I Tested These Changes

bk
  • Loading branch information
alangenfeld authored Nov 7, 2024
1 parent 6862cc6 commit 23824c1
Show file tree
Hide file tree
Showing 54 changed files with 67 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ To define multiple, similar asset checks, use a factory pattern. In the followin

```python file=/concepts/assets/asset_checks/factory.py
from typing import Any, Mapping, Sequence

from mock import MagicMock
from unittest.mock import MagicMock

from dagster import (
AssetCheckResult,
Expand Down
4 changes: 2 additions & 2 deletions docs/content/concepts/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Mock resources can be provided directly using `materialize_to_memory`:

```python file=/concepts/ops_jobs_graphs/unit_tests.py startafter=start_materialize_resources endbefore=end_materialize_resources
from dagster import asset, materialize_to_memory, ConfigurableResource
import mock
from unittest import mock


class MyServiceResource(ConfigurableResource): ...
Expand Down Expand Up @@ -384,7 +384,7 @@ If your ops rely on more complex resources, such as those that build separate cl

```python file=/concepts/resources/pythonic_resources.py startafter=start_new_resource_testing_with_state_ops endbefore=end_new_resource_testing_with_state_ops dedent=4
from dagster import ConfigurableResource, op
import mock
from unittest import mock

class MyClient:
...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from unittest import mock

from dagster_aws.s3 import S3FileHandle, S3FileManager

import dagster as dg
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from unittest import mock

from dagster_aws.s3 import S3FileHandle, S3FileManager

import dagster as dg
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any, Mapping, Sequence

from mock import MagicMock
from unittest.mock import MagicMock

from dagster import (
AssetCheckResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Definitions,
OpExecutionContext,
)
from mock import MagicMock
from unittest.mock import MagicMock


def create_db_connection():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def context_op(context: OpExecutionContext):
# start_test_op_with_resource_client

from dagster import ConfigurableResource
import mock
from unittest import mock


class Client:
Expand Down Expand Up @@ -407,7 +407,7 @@ def test_data_assets():

# start_materialize_resources
from dagster import asset, materialize_to_memory, ConfigurableResource
import mock
from unittest import mock


class MyServiceResource(ConfigurableResource): ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def new_resource_testing_with_state_ops() -> None:
# start_new_resource_testing_with_state_ops

from dagster import ConfigurableResource, op
import mock
from unittest import mock

class MyClient:
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def scope_airbyte_cloud_manual_config():


def scope_add_downstream_assets():
import mock
from unittest import mock

with mock.patch("dagster_snowflake_pandas.SnowflakePandasIOManager"):
# start_add_downstream_assets
Expand Down Expand Up @@ -158,7 +158,7 @@ def stargazers_file(stargazers: pd.DataFrame):


def scope_add_downstream_assets_w_deps():
import mock
from unittest import mock

with mock.patch("dagster_snowflake.SnowflakeResource"):
# start_with_deps_add_downstream_assets
Expand Down Expand Up @@ -209,7 +209,7 @@ def stargazers_file(snowflake: SnowflakeResource):


def scope_add_downstream_assets_cloud():
import mock
from unittest import mock

with mock.patch("dagster_snowflake_pandas.SnowflakePandasIOManager"):
# start_add_downstream_assets_cloud
Expand Down Expand Up @@ -263,7 +263,7 @@ def stargazers_file(stargazers: pd.DataFrame):


def scope_add_downstream_assets_cloud_with_deps():
import mock
from unittest import mock

with mock.patch("dagster_snowflake.SnowflakeResource"):
# start_with_deps_add_downstream_assets_cloud
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def scope_schedule_assets():


def scope_add_downstream_assets():
import mock
from unittest import mock

with mock.patch("dagster_snowflake_pandas.SnowflakePandasIOManager"):
# start_add_downstream_assets
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime
from importlib import resources
from unittest import mock
from unittest.mock import MagicMock

import mock
import pytest
from dagster_tests.cli_tests.command_tests.assets import fail_asset
from mock import MagicMock

from dagster import Definitions
from dagster._core.definitions.asset_check_spec import AssetCheckKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock
from unittest import mock

from docs_snippets.concepts.assets.asset_config import (
MyDownstreamAssetConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock
from unittest import mock


def test_uses_resource() -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any
from unittest import mock

import mock
import pytest

from dagster._core.definitions.run_config import RunConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import cast
from unittest import mock

import mock
from dagster import (
AssetKey,
AssetsDefinition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta, timezone
from typing import Optional, Sequence
from unittest import mock

import mock
import pytest
from dagster import (
AssetCheckKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import mock

import boto3
import mock
from dagster_airlift.mwaa import MwaaSessionAuthBackend


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List
from unittest import mock

import mock
import pytest
from click.testing import CliRunner

Expand Down
1 change: 0 additions & 1 deletion examples/project_fully_featured/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"dbt-duckdb",
"dbt-snowflake",
"duckdb!=0.3.3, <= 6.0.0", # missing wheels
"mock",
"pandas",
"pyarrow>=4.0.0",
"pyspark",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, Tuple
from unittest import mock

import mock
from dagster import (
AssetKey,
DailyPartitionsDefinition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from unittest import mock

from dagster_graphql.test.utils import execute_dagster_graphql

SET_NUX_SEEN_MUTATION = """
Expand Down
3 changes: 2 additions & 1 deletion python_modules/dagster-test/dagster_test_tests/test_toys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from unittest import mock

import pytest
from dagster import (
DagsterEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys
import time
from typing import List
from unittest import mock

import mock
import pytest
from dagster import AssetIn, AssetKey, Definitions, asset
from dagster._core.definitions.resolved_asset_deps import resolve_similar_asset_names
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import sys
from unittest import mock

import mock
import pytest
from dagster import AssetSelection, Definitions, asset, define_asset_job
from dagster._core.errors import DagsterInvalidSubsetError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import tempfile
from contextlib import contextmanager
from typing import ContextManager, NoReturn, Optional, Tuple
from unittest import mock

import mock
import pytest
from click.testing import CliRunner
from dagster import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from unittest import mock

import click
import mock
import pytest
from click.testing import CliRunner
from dagster._cli.schedule import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from unittest import mock

import click
import mock
import pytest
from click.testing import CliRunner
from dagster._cli.sensor import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from unittest import mock

import mock
import pytest
from click.testing import CliRunner
from dagster import DagsterEventType, job, op, reconstructable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from contextlib import ExitStack
from unittest import mock

import mock
import pytest
from dagster import job, op
from dagster._core.execution.plan.instance_concurrency_context import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from unittest import mock

import mock
import pytest
from dagster import (
DagsterInstance,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import enum
from abc import ABC, abstractmethod
from typing import List, Mapping, Optional
from unittest import mock

import mock
import pytest
from dagster import (
AssetExecutionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import random
from collections import defaultdict
from typing import List, NamedTuple, Optional
from unittest import mock

import mock
import pytest
from dagster import (
AssetKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
from unittest import mock

import mock
import pytest
import responses
from click.testing import CliRunner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import multiprocessing
from unittest.mock import patch

import pytest
from dagster._core.definitions.run_request import InstigatorType
Expand All @@ -19,7 +20,6 @@
from dagster._seven import IS_WINDOWS
from dagster._time import create_datetime, get_timezone
from dagster._vendored.dateutil.relativedelta import relativedelta
from mock import patch

from dagster_tests.daemon_sensor_tests.conftest import create_workspace_load_target
from dagster_tests.daemon_sensor_tests.test_sensor_run import wait_for_all_runs_to_start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from contextlib import ExitStack
from typing import Any
from unittest import mock
from unittest.mock import patch

import pytest
from dagster import (
Expand Down Expand Up @@ -94,7 +95,6 @@
from dagster._record import copy
from dagster._time import create_datetime, get_current_datetime
from dagster._vendored.dateutil.relativedelta import relativedelta
from mock import patch

from dagster_tests.daemon_sensor_tests.conftest import create_workspace_load_target

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import string
import sys
import time
from unittest import mock

import dagster._check as check
import mock
import pytest
from dagster import (
AllPartitionMapping,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
Tuple,
Union,
)
from unittest import mock

import dagster._check as check
import mock
import pytest
from dagster import (
AssetIn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from contextlib import contextmanager
from dataclasses import dataclass, field
from typing import AbstractSet, Iterable, NamedTuple, Optional, Sequence, Union, cast
from unittest import mock

import mock
from dagster import (
AssetExecutionContext,
AssetKey,
Expand Down
Loading

2 comments on commit 23824c1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for dagster-docs ready!

✅ Preview
https://dagster-docs-31iz9sn8n-elementl.vercel.app
https://master.dagster.dagster-docs.io

Built with commit 23824c1.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link

@github-actions github-actions bot commented on 23824c1 Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for dagster-docs-beta ready!

✅ Preview
https://dagster-docs-beta-3vvxx0xzo-elementl.vercel.app

Built with commit 23824c1.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.