11import re
22from random import choice , randint
33from string import ascii_uppercase
4- from typing import Optional
4+ from typing import Literal
55
66# FORMATTING
77############
88
99
10- def convert_to_mercosul (license_plate : str ) -> Optional [ str ] :
10+ def convert_to_mercosul (license_plate : str ) -> str | None :
1111 """
1212 Converts an old pattern license plate (LLLNNNN) to a Mercosul format
1313 (LLLNLNN).
@@ -17,7 +17,7 @@ def convert_to_mercosul(license_plate: str) -> Optional[str]:
1717 old pattern license plate.
1818
1919 Returns:
20- Optional[ str] : The converted Mercosul license plate (LLLNLNN) or
20+ str | None : The converted Mercosul license plate (LLLNLNN) or
2121 'None' if the input is invalid.
2222
2323 Example:
@@ -34,7 +34,7 @@ def convert_to_mercosul(license_plate: str) -> Optional[str]:
3434 return "" .join (digits )
3535
3636
37- def format_license_plate (license_plate : str ) -> Optional [ str ] :
37+ def format_license_plate (license_plate : str ) -> str | None :
3838 """
3939 Formats a license plate into the correct pattern.
4040 This function receives a license plate in any pattern (LLLNNNN or LLLNLNN)
@@ -44,7 +44,7 @@ def format_license_plate(license_plate: str) -> Optional[str]:
4444 license_plate (str): A license plate string.
4545
4646 Returns:
47- Optional[ str] : The formatted license plate string or 'None' if the
47+ str | None : The formatted license plate string or 'None' if the
4848 input is invalid.
4949
5050 Example:
@@ -69,14 +69,16 @@ def format_license_plate(license_plate: str) -> Optional[str]:
6969############
7070
7171
72- def is_valid (license_plate , type = None ): # type: (str, str) -> bool
72+ def is_valid (
73+ license_plate : str , type : Literal ["old_format" , "mercosul" ] | None = None
74+ ) -> bool :
7375 """
7476 Returns if a Brazilian license plate number is valid.
7577 It does not verify if the plate actually exists.
7678
7779 Args:
7880 license_plate (str): The license plate number to be validated.
79- type (str ): "old_format" or "mercosul".
81+ type (Literal["old_format", "mercosul"] | None ): "old_format" or "mercosul".
8082 If not specified, checks for one or another.
8183 Returns:
8284 bool: True if the plate number is valid. False otherwise.
@@ -101,7 +103,7 @@ def remove_symbols(license_plate_number: str) -> str:
101103 be removed.
102104
103105 Returns:
104- str: The license plate number with the specified symbols removed.
106+ str | None : The license plate number with the specified symbols removed.
105107
106108 Example:
107109 >>> remove_symbols("ABC-123")
@@ -115,7 +117,7 @@ def remove_symbols(license_plate_number: str) -> str:
115117 return license_plate_number .replace ("-" , "" )
116118
117119
118- def get_format (license_plate : str ) -> Optional [ str ] :
120+ def get_format (license_plate : str ) -> str | None :
119121 """
120122 Return the format of a license plate. 'LLLNNNN' for the old pattern and
121123 'LLLNLNN' for the Mercosul one.
@@ -124,7 +126,7 @@ def get_format(license_plate: str) -> Optional[str]:
124126 license_plate (str): A license plate string without symbols.
125127
126128 Returns:
127- str: The format of the license plate (LLLNNNN, LLLNLNN) or
129+ str | None : The format of the license plate (LLLNNNN, LLLNLNN) or
128130 'None' if the format is invalid.
129131
130132 Example:
@@ -145,7 +147,7 @@ def get_format(license_plate: str) -> Optional[str]:
145147 return None
146148
147149
148- def generate (format = "LLLNLNN" ): # type: (str) -> str | None
150+ def generate (format : str = "LLLNLNN" ) -> str | None :
149151 """
150152 Generate a valid license plate in the given format. In case no format is
151153 provided, it will return a license plate in the Mercosul format.
@@ -156,7 +158,7 @@ def generate(format="LLLNLNN"): # type: (str) -> str | None
156158 Mercosul one. Default is 'LLLNLNN'
157159
158160 Returns:
159- str: A randomly generated license plate number or
161+ str | None : A randomly generated license plate number or
160162 'None' if the format is invalid.
161163
162164 Example:
0 commit comments