Skip to content

feat: Add CaPostalCodeRecognizer for Canadian postal codes (CA_POSTAL… - #2204

Merged
SharonHart merged 3 commits into
data-privacy-stack:mainfrom
watsos:feature/ca-postal-code-recognizer
Jul 29, 2026
Merged

feat: Add CaPostalCodeRecognizer for Canadian postal codes (CA_POSTAL…#2204
SharonHart merged 3 commits into
data-privacy-stack:mainfrom
watsos:feature/ca-postal-code-recognizer

Conversation

@watsos

@watsos watsos commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

…_CODE)

Change Description

Adds a new predefined country-specific recognizer, CaPostalCodeRecognizer, to detect Canadian postal codes in the standard A1A 1A1 format (entity type CA_POSTAL_CODE).

Presidio has recognizers for other Canadian identifiers (CaSinRecognizer) but no recognizer for postal codes, despite them being a common identifier in addresses, shipping records, and customer data.

Details:

  • Supports the standard A1A 1A1 format (letter-digit-letter, space, digit-letter-digit)
  • Two-tier pattern scoring: the canonical spaced form (K1A 0A1) scores 0.3; the space-omitted form (K1A0A1) scores 0.1, relying on surrounding context to clear the analyzer's score threshold, since the unspaced shape is more prone to coincidentally matching unrelated 6-character alphanumeric codes
  • Validates letter positions per Canada Post's official spec: D, F, I, O, Q, U are excluded from all letter positions; W and Z are additionally excluded from the first letter position only (reserved for future expansion)
  • Case-insensitive matching (relies on PatternRecognizer's default IGNORECASE flag), since PII may appear in lowercase in real-world text even though Canada Post's official format is uppercase
  • Context words include English and French terms (postal code, code postal, province names), following the bilingual convention used by CaSinRecognizer
  • Disabled by default (enabled: false), matching the convention for other country-specific recognizers

Issue reference

Fixes #2163

Checklist

  • I have reviewed the contribution guidelines
  • I agree to follow this project's Code of Conduct
  • I confirm that I have the right to submit this contribution and that it does not knowingly contain proprietary or confidential code.
  • My code includes unit tests
  • All unit tests and lint checks pass locally
  • My PR contains documentation updates / additions if required

Copilot AI review requested due to automatic review settings July 28, 2026 06:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new predefined, Canada-specific analyzer recognizer to detect Canadian postal codes (CA_POSTAL_CODE) and wires it into Presidio’s predefined recognizer registry, with accompanying unit tests and documentation.

Changes:

  • Introduces CaPostalCodeRecognizer with strict (spaced) and weak (unspaced) regex patterns plus context terms.
  • Registers the recognizer in the Canada package, the predefined recognizers package exports, and default_recognizers.yaml (disabled by default).
  • Adds unit tests and documents the new CA_POSTAL_CODE entity in docs/supported_entities.md.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/canada/ca_postal_code_recognizer.py Implements CaPostalCodeRecognizer patterns/context and country tagging.
presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/canada/init.py Exposes CaPostalCodeRecognizer from the Canada country-specific package.
presidio-analyzer/presidio_analyzer/predefined_recognizers/init.py Exports CaPostalCodeRecognizer from the predefined recognizers package.
presidio-analyzer/presidio_analyzer/conf/default_recognizers.yaml Adds the recognizer to the default registry configuration (disabled).
presidio-analyzer/tests/test_ca_postal_code_recognizer.py Adds unit tests covering valid/invalid cases and boundary behavior.
docs/supported_entities.md Documents the new CA_POSTAL_CODE supported entity.

Comment thread presidio-analyzer/presidio_analyzer/conf/default_recognizers.yaml
Copilot AI review requested due to automatic review settings July 28, 2026 06:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

presidio-analyzer/presidio_analyzer/predefined_recognizers/init.py:16

  • CaPostalCodeRecognizer is exported from presidio_analyzer.predefined_recognizers, but the umbrella package presidio_analyzer.predefined_recognizers.country_specific still only exports CaSinRecognizer (see predefined_recognizers/country_specific/__init__.py). If users rely on from presidio_analyzer.predefined_recognizers.country_specific import ..., this new recognizer won’t be available; consider updating that package’s __init__.py / __all__ to include CaPostalCodeRecognizer as well.
# Canada recognizers
from .country_specific.canada.ca_postal_code_recognizer import CaPostalCodeRecognizer
from .country_specific.canada.ca_sin_recognizer import CaSinRecognizer

@SharonHart
SharonHart merged commit 2bb88d2 into data-privacy-stack:main Jul 29, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add Canadian postal code recognizer (CA_POSTAL_CODE)

4 participants