Skip to content

Commit

Permalink
Improved TypeError exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paebbels committed Jun 8, 2024
1 parent 930aeed commit b0aac44
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions pyVHDLModel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@

from enum import unique, Enum, Flag, auto
from pathlib import Path
from sys import version_info

from typing import Union, Dict, cast, List, Generator, Optional as Nullable

from pyTooling.Common import getFullyQualifiedName
from pyTooling.Decorators import export
from pyTooling.Graph import Graph, Vertex, Edge

Expand Down Expand Up @@ -1483,7 +1485,10 @@ def __init__(self, path: Path, documentation: str = None):

def _AddEntity(self, item: Entity) -> None:
if not isinstance(item, Entity):
raise TypeError(f"Parameter 'item' is not of type 'Entity'.")
ex = TypeError(f"Parameter 'item' is not of type 'Entity'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._entities:
Expand All @@ -1496,7 +1501,10 @@ def _AddEntity(self, item: Entity) -> None:

def _AddArchitecture(self, item: Architecture) -> None:
if not isinstance(item, Architecture):
raise TypeError(f"Parameter 'item' is not of type 'Architecture'.")
ex = TypeError(f"Parameter 'item' is not of type 'Architecture'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

entity = item.Entity.Name
entityIdentifier = entity.NormalizedIdentifier
Expand All @@ -1514,7 +1522,10 @@ def _AddArchitecture(self, item: Architecture) -> None:

def _AddPackage(self, item: Package) -> None:
if not isinstance(item, (Package, PackageInstantiation)):
raise TypeError(f"Parameter 'item' is not of type 'Package' or 'PackageInstantiation'.")
ex = TypeError(f"Parameter 'item' is not of type 'Package' or 'PackageInstantiation'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._packages:
Expand All @@ -1526,7 +1537,10 @@ def _AddPackage(self, item: Package) -> None:

def _AddPackageBody(self, item: PackageBody) -> None:
if not isinstance(item, PackageBody):
raise TypeError(f"Parameter 'item' is not of type 'PackageBody'.")
ex = TypeError(f"Parameter 'item' is not of type 'PackageBody'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._packageBodies:
Expand All @@ -1538,7 +1552,10 @@ def _AddPackageBody(self, item: PackageBody) -> None:

def _AddContext(self, item: Context) -> None:
if not isinstance(item, Context):
raise TypeError(f"Parameter 'item' is not of type 'Context'.")
ex = TypeError(f"Parameter 'item' is not of type 'Context'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._contexts:
Expand All @@ -1550,7 +1567,10 @@ def _AddContext(self, item: Context) -> None:

def _AddConfiguration(self, item: Configuration) -> None:
if not isinstance(item, Configuration):
raise TypeError(f"Parameter 'item' is not of type 'Configuration'.")
ex = TypeError(f"Parameter 'item' is not of type 'Configuration'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._configurations:
Expand All @@ -1562,7 +1582,10 @@ def _AddConfiguration(self, item: Configuration) -> None:

def _AddVerificationUnit(self, item: VerificationUnit) -> None:
if not isinstance(item, VerificationUnit):
raise TypeError(f"Parameter 'item' is not of type 'VerificationUnit'.")
ex = TypeError(f"Parameter 'item' is not of type 'VerificationUnit'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._verificationUnits:
Expand All @@ -1574,7 +1597,10 @@ def _AddVerificationUnit(self, item: VerificationUnit) -> None:

def _AddVerificationProperty(self, item: VerificationProperty) -> None:
if not isinstance(item, VerificationProperty):
raise TypeError(f"Parameter 'item' is not of type 'VerificationProperty'.")
ex = TypeError(f"Parameter 'item' is not of type 'VerificationProperty'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._verificationProperties:
Expand All @@ -1586,7 +1612,10 @@ def _AddVerificationProperty(self, item: VerificationProperty) -> None:

def _AddVerificationMode(self, item: VerificationMode) -> None:
if not isinstance(item, VerificationMode):
raise TypeError(f"Parameter 'item' is not of type 'VerificationMode'.")
ex = TypeError(f"Parameter 'item' is not of type 'VerificationMode'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

identifier = item.NormalizedIdentifier
if identifier in self._verificationModes:
Expand Down Expand Up @@ -1625,9 +1654,15 @@ def _AddDesignUnit(self, item: DesignUnit) -> None:
elif isinstance(item, VerificationMode):
self._verificationModes[identifier] = item
elif isinstance(item, DesignUnit):
raise TypeError(f"Parameter 'item' is an unknown 'DesignUnit'.")
ex = TypeError(f"Parameter 'item' is an unknown 'DesignUnit'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex
else:
raise TypeError(f"Parameter 'item' is not of type 'DesignUnit'.")
ex = TypeError(f"Parameter 'item' is not of type 'DesignUnit'.")
if version_info >= (3, 11): # pragma: no cover
ex.add_note(f"Got type '{getFullyQualifiedName(item)}'.")
raise ex

self._designUnits.append(item)
item._parent = self
Expand Down

0 comments on commit b0aac44

Please sign in to comment.