python-scala-generics-variance
/python-scala-generics-variance
Translating Python generics, variance annotations, protocols, and type variables to Scala type parameters, variance annotations, and traits. Use when converting Python Generic[T], TypeVar with covariant/contravariant, Protocol classes, or higher-kinded type simulations to Scala.
- Author
- cxcscmu ↗
- Repository
- SkillLearnBench ↗
- Licence
- MIT
- Last repository change
- 02.10.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 02.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Translating Python generics, variance annotations, protocols, and type variables to Scala type parameters, variance annotations, and traits. Use when converting Python Generic[T], TypeVar with covariant/contravariant, Protocol classes, or higher-kinded type simulations to Scala.
Original author description
Translating Python generics, variance annotations, protocols, and type variables to Scala type parameters, variance annotations, and traits. Use when converting Python Generic[T], TypeVar with covariant/contravariant, Protocol classes, or higher-kinded type simulations to Scala.
How it works
- Python Generic[T] → Scala trait/class with type parameter
- Variance Annotations
- Python Protocol → Scala trait (structural typing)
- Bounded TypeVars → Scala type bounds or overloading
- Higher-Kinded Type Simulation → Scala class with type param
SKILL.md preview
---
name: python-scala-generics-variance
description: >
Translating Python generics, variance annotations, protocols, and type variables
to Scala type parameters, variance annotations, and traits. Use when converting
Python Generic[T], TypeVar with covariant/contravariant, Protocol classes, or
higher-kinded type simulations to Scala.
---
# Python Generics & Variance to Scala
## Python Generic[T] → Scala trait/class with type parameter
Python:
```python
T = TypeVar("T")
class BaseTokenizer(ABC, Generic[T]):
@abstractmethod
def tokenize(self, value: T) -> Token: ...
```
Scala:
```scala
trait BaseTokenizer[T] {
def tokenize(value: T): Token
def tokenizeBatch(values: Iterable[T]): Iterator[Token] =
values.iterator.map(tokenize)
}
```
Key: Python's `ABC + Generic[T]` becomes a Scala `trait[T]`. Abstract methods need no `abstract` keyword in traits — just leave them unimplemented.
## Variance Annotations
Python:
```python
T_co = TypeVar("T_co", covariant=True)
T_contra = TypeVar("T_contra", contravariant=True)
```
Scala:
```scala
class TokenContainer[+A](items: Seq[A]) // covariant
class TokenSink[-A] // contravariant
class BivariantHandler[A](default: A) // invariant
```
## Python Protocol → Scala trait (structural typing)
Python protocols are structural types. In Scala, use regular traits:
```scala
trait Tokenizable {
def toToken: String
}
```
## Bounded TypeVars → Scala type bounds or overloading
Python `TypeVar("NumericT", int, float, Decimal)` constrains to specific types. In Scala, use overloaded methods, union-style sealed traits, or context bounds depending on context.
For `TypeVar("StrOrBytes", str, bytes)`:
- If runtime dispatch is needed, use pattern matching on `Any` or overloaded methods
## Higher-Kinded Type Simulation → Scala class with type param
Python's `TokenFunctor` simulation translates naturally:
```scala
class TokenFunctor[A](private val value: A) {
def map[B](f: A => B): TokenFunctor[B] = new TokenFunctor(f(value))
def flatMap[B](f: A => TokenFunctor[B]): TokenFunctor[B] = f(value)
def get: A = value
}
```
Note: `getOrElse` with null-checking becomes Option-based in idiomatic Scala.
Versions
The database has no other skills named python-scala-generics-variance.
Database snapshot: 13.11.2026 · package version 73792d092653 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100scala-generics-variance
How to handle type variance in Scala generics when translating from Python's flexible type system. Use this whenever translating Python generic types (TypeVar with covariant/contravariant bounds) to Scala. Covers covariance (+T), contravariance (-T), and invariance for proper type safety. - 95python-scala-functional-generics
Guide for translating Python generic containers (covariant/contravariant), functor/monad simulations, and JSON handling to idiomatic Scala. Use this skill when converting Python Generic[T_co], Generic[T_contra], TokenFunctor, TokenMonad, or json.dumps/json.loads patterns to… - 85Scala Type System
Use when scala's advanced type system including generics, variance, type bounds, implicit conversions, type classes, higher-kinded types, path-dependent types, and abstract type members for building type-safe, flexible APIs. - 100python-type-safety
Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright. - 100python-type-safety
Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright. - 95python-type-safety
Modern Python type safety with type hints, generics, protocols, and strict type checking using ty and ruff. Use when adding type annotations, implementing generic classes, defining structural interfaces, configuring ty/ruff, or writing type-safe Python 3.13+/3.14+ code. Triggers…