{"id":2332,"library":"typepy","title":"typepy: Runtime Type Checker, Validator, and Converter","description":"typepy is a Python library for variable type checking, validation, and conversion at runtime. As of version 1.3.4, it supports Python 3.9 and newer. It maintains an active release cadence, with several minor updates per year addressing compatibility, new Python versions, and bug fixes.","status":"active","version":"1.3.4","language":"en","source_language":"en","source_url":"https://github.com/thombashi/typepy","tags":["type checking","type conversion","validation","data types"],"install":[{"cmd":"pip install typepy","lang":"bash","label":"Install typepy"}],"dependencies":[],"imports":[{"note":"The primary class for generic type handling and conversion with constants like Type.INTEGER.","symbol":"Type","correct":"import typepy\ntc = typepy.Type(...)"},{"symbol":"String","correct":"from typepy import String\ns = String(' value ')"},{"symbol":"Integer","correct":"from typepy import Integer\ni = Integer('123')"},{"note":"Specific type classes are available for direct use when precise type handling is needed.","symbol":"DateTime","correct":"from typepy import DateTime\ndt = DateTime('2023-01-01')"}],"quickstart":{"code":"import typepy\n\n# Generic type checking and conversion\ntc = typepy.Type('100')\nprint(f\"Converted '100' to integer: {tc.try_get(typepy.Type.INTEGER)}\")\n\ntc_none = typepy.Type(None)\nprint(f\"Converted None to string with default: {tc_none.try_get(typepy.Type.STRING, default_value='a')}\")\n\n# Using specific type classes\nstr_value = typepy.String('  hello  ').strip()\nprint(f\"Stripped string: '{str_value}'\")\n\nint_value = typepy.Integer('1,234').convert()\nprint(f\"Converted '1,234' to integer: {int_value}\")\n\ntry:\n    # Conversion without try_get will raise an error on failure\n    invalid_int = typepy.Integer('abc').convert()\n    print(f\"Converted 'abc' to integer: {invalid_int}\")\nexcept typepy.TypeConversionError as e:\n    print(f\"Error converting 'abc' to integer: {e}\")","lang":"python","description":"This example demonstrates how to use the `typepy.Type` class for generic type handling and conversion, including providing default values for `None`. It also shows the usage of specific type classes like `typepy.String` and `typepy.Integer` for more focused operations and how to handle potential conversion errors."},"warnings":[{"fix":"Upgrade your Python interpreter to version 3.9 or a later compatible version.","message":"Support for Python 3.7 and 3.8 was dropped in version 1.3.3. Ensure your environment uses Python 3.9 or newer.","severity":"breaking","affected_versions":">=1.3.3"},{"fix":"Replace all instances of `typepy.Binary` with `typepy.Bytes`.","message":"The `Binary` type class was renamed to `Bytes` in version 1.1.0 to align with Python 3's `bytes` type. Usage of `Binary` will result in an `AttributeError`.","severity":"breaking","affected_versions":">=1.1.0"},{"fix":"Upgrade your Python interpreter to version 3.7 or a later compatible version (preferably 3.9+ for current versions).","message":"Support for Python 3.6 was dropped in version 1.3.1. `typepy` now requires Python 3.7 (or later, as of 1.3.3).","severity":"breaking","affected_versions":">=1.3.1"},{"fix":"Use `Type(...).try_get(Type.TARGET_TYPE, default_value=...)` when you want to gracefully handle conversion failures with a fallback. Use `Type(...).convert()` (or specific type class `.convert()`) when you expect strict conversion or want to catch and handle specific conversion errors.","message":"When performing type conversions, `convert()` will raise a `typepy.TypeConversionError` on failure, while `try_get()` allows specifying a `default_value` to return instead of raising an error.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}