Skip to content
Skills Library
Programming · Conversion & formats

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.

Licence
MIT
Last repository change
02.10.2026

Assessment and security

95/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content30 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 02.10.2026, without references to retired models or APIs.

Licence

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

  1. Python Generic[T] → Scala trait/class with type parameter
  2. Variance Annotations
  3. Python Protocol → Scala trait (structural typing)
  4. Bounded TypeVars → Scala type bounds or overloading
  5. Higher-Kinded Type Simulation → Scala class with type param
Original SKILL.md headings · 68 lines, 235 words
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…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.