Skip to content

Commit 958e394

Browse files
authored
fix: mutable defaults in init (#48)
1 parent 0c8c6a4 commit 958e394

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/amplitude/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ class Amplitude:
3737
shutdown(): Shutdown the client instance
3838
"""
3939

40-
def __init__(self, api_key: str, configuration: Config = Config()):
40+
def __init__(self, api_key: str, configuration: Optional[Config] = None):
4141
"""The constructor for the Amplitude class
4242
4343
Args:
4444
api_key (str): The api key of amplitude project. Must be set properly before tracking events.
4545
configuration (amplitude.config.Config, optional): The configuration of client instance. A new instance
4646
with default config value will be used by default.
4747
"""
48-
self.configuration: Config = configuration
48+
self.configuration: Config = configuration or Config()
4949
self.configuration.api_key = api_key
5050
self.__timeline = Timeline()
5151
self.__timeline.setup(self)

src/amplitude/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, api_key: str = None,
5555
server_zone: str = constants.DEFAULT_ZONE,
5656
use_batch: bool = False,
5757
server_url: Optional[str] = None,
58-
storage_provider: StorageProvider = InMemoryStorageProvider(),
58+
storage_provider: Optional[StorageProvider] = None,
5959
plan: Plan = None,
6060
ingestion_metadata: IngestionMetadata = None):
6161
"""The constructor of Config class"""
@@ -70,7 +70,7 @@ def __init__(self, api_key: str = None,
7070
self.server_zone: str = server_zone
7171
self.use_batch: bool = use_batch
7272
self._url: Optional[str] = server_url
73-
self.storage_provider: StorageProvider = storage_provider
73+
self.storage_provider: StorageProvider = storage_provider or InMemoryStorageProvider()
7474
self.opt_out: bool = False
7575
self.plan: Plan = plan
7676
self.ingestion_metadata: IngestionMetadata = ingestion_metadata

src/test/test_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ def setUp(self) -> None:
1616

1717
def tearDown(self) -> None:
1818
self.client.shutdown()
19+
20+
def test_amplitude_client_init_multiple_instance(self):
21+
# test multiple instance inited with different API keys
22+
client1 = Amplitude(api_key="test api key 1")
23+
client2 = Amplitude(api_key="test api key 2")
24+
self.assertNotEqual(client1.configuration.api_key, client2.configuration.api_key)
25+
self.assertNotEqual(client1.configuration.storage_provider, client2.configuration.storage_provider)
1926

2027
def test_amplitude_client_track_success(self):
2128
post_method = MagicMock()

src/test/test_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ def test_config_flush_queue_size_decrease_no_less_than_one(self):
120120
self.assertEqual(1, config.flush_queue_size)
121121
config._increase_flush_divider()
122122
self.assertEqual(1, config.flush_queue_size)
123-
123+
124+
def test_config_multiple_instances_has_different_storage_provider(self):
125+
config1 = Config()
126+
config2 = Config()
127+
self.assertIsNot(config1.storage_provider, config2.storage_provider)
124128

125129
if __name__ == '__main__':
126130
unittest.main()

0 commit comments

Comments
 (0)