Collecting data with Trackers and Webhooks

  1. Home
  2. Docs
  3. Collecting data with Trackers and Webhooks
  4. Trackers – collecting data from your own applications
  5. Python Tracker
  6. Contracts

Contracts

Python is a dynamically typed language, but each of our methods expects its arguments to be of specific types and value ranges. To avoid wrong inputs, the tracker takes two approaches:

  1. It specifies type hints on each function argument and return value. This allows IDEs to validate function calls in your code and also enables static type checking using tools such as mypy. However, type violations do not result in runtime errors.
  2. It validates expected value ranges during runtime and throws ValueError errors in case they are violated. Contracts are defined for arguments to check that they belong to expected value ranges such as:
s = Subject() t.set_platform("coffee") # throws ValueError because "coffee" is not one of supported platforms t.set_screen_resolution(width=-1, height=-1) # throws ValueError because width and height arguments have to be greater than 0
Code language: Python (python)

You can turn off runtime contract validation like this:

from snowplow_tracker import disable_contracts disable_contracts()
Code language: JavaScript (javascript)

Contracts in tracker version 0.9.1

Earlier versions of the tracker up to v0.9.1 used the PyContracts library to validate argument types and value ranges. In contrast with the latest versions, pycontracts was also used to validate argument types, raising ContractNotRespected errors at runtime when passing values with wrong types (this behavior was dropped in favor of type hints in Python 3):

s = Subject() t.set_color_depth("walrus")
Code language: JavaScript (javascript)
contracts.interface.ContractNotRespected: Breach for argument 'depth' to Subject:set_color_depth(). Expected type 'int', got 'str'. checking: Int for value: Instance of str: 'walrus' checking: $(Int) for value: Instance of str: 'walrus' checking: int for value: Instance of str: 'walrus' Variables bound in inner context: - self: Instance of Tracker: <snowplow_tracker.tracker.Tracker object...> [clip]
Code language: JavaScript (javascript)

If your value is of the wrong type, convert it before passing it into the track...() method, for example:

level_idx = 42 t.track_screen_view("Game Level", str(level_idx))
Code language: JavaScript (javascript)

You can turn off type checking to improve performance like this:

from snowplow_tracker import disable_contracts disable_contracts()
Code language: JavaScript (javascript)

If you’d like to learn more about Snowplow BDP you can book a demo with our team, or if you’d prefer, you can try Snowplow technology for yourself quickly and easily.