API Reference¶
Core Components¶
NewType Factory¶
newtype.NewType(base_type, **_context)
¶
Create a new type that preserves type information through all operations.
This is the main factory function for creating new types. It wraps an existing type and ensures that all operations on instances of the new type preserve their type information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_type |
T
|
The base type to wrap |
required |
**context |
Additional context for type creation (reserved for future use) |
required |
Returns¶
A new type that inherits from the base type and preserves type information
Example
Technical Details
- Uses a global cache to prevent duplicate type creation
- Properly handles slots and descriptors
- Maintains all original type functionality
- Provides proper type hints for IDE support
Source code in newtype/newtype.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
newtype_exclude Decorator¶
newtype.newtype_exclude(func)
¶
Decorator to exclude a method from type wrapping.
This decorator marks methods that should not be wrapped by NewTypeMethod, allowing them to maintain their original behavior without type preservation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[..., Any]
|
The function to be excluded from type wrapping |
required |
Returns¶
The original function, marked with an exclusion flag
Example
Source code in newtype/newtype.py
Type System Components¶
These are internal components that power python-newtype. While they're not typically used directly, understanding them can be helpful for advanced usage or debugging.
NewTypeInit Descriptor¶
newtype.NewTypeInit
¶
Descriptor class for handling NewType subclass initialization.
This class is responsible for intercepting instance creation of NewType subclasses and ensuring proper type preservation. It implements both the descriptor protocol and the callable interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[..., Any]
|
The initialization function to be wrapped, typically the init method of the NewType subclass. |
required |
Attributes¶
func_get: The bound method if the wrapped function is a descriptor
has_get: Boolean indicating if the wrapped function has __get__
obj: The instance being initialized (None for unbound calls)
cls: The class being initialized
Source code in newtype/extensions/newtypeinit.pyi
__call__(*args, **kwargs)
¶
Handle the actual initialization call.
This method is called when creating a new instance of a NewType subclass. It ensures that: 1. The parent class's new is called properly 2. The instance's init is called with the provided arguments 3. The correct type is preserved throughout the process
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Any
|
Positional arguments for initialization |
()
|
**kwargs |
Any
|
Keyword arguments for initialization |
{}
|
Returns¶
A properly initialized instance of the NewType subclass
Source code in newtype/extensions/newtypeinit.pyi
__get__(inst, owner)
¶
Implement the descriptor protocol for method binding.
This method is called when accessing the initialization method on either the class or an instance. It ensures proper method binding and type preservation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inst |
Any | None
|
The instance the method is being accessed from (None for class access) |
required |
owner |
type[Any] | None
|
The class that owns this method |
required |
Returns¶
A bound or unbound NewTypeInit instance
Source code in newtype/extensions/newtypeinit.pyi
NewTypeMethod Descriptor¶
newtype.extensions.NewTypeMethod
¶
Descriptor class for handling NewType subclass method calls.
This class is responsible for intercepting method calls on NewType subclasses and ensuring proper type preservation. It implements both the descriptor protocol and the callable interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[..., Any]
|
The method to be wrapped |
required |
wrapped_cls |
Type[Any]
|
The NewType subclass that owns this method |
required |
Attributes¶
func_get: The bound method if the wrapped function is a descriptor
has_get: Boolean indicating if the wrapped function has __get__
obj: The instance the method is bound to (None for unbound calls)
cls: The class that owns this method
wrapped_cls: The NewType subclass this method belongs to
Source code in newtype/extensions/newtypemethod.pyi
__call__(*args, **kwargs)
¶
Handle the actual method call.
This method is called when invoking a method on a NewType subclass instance. It ensures that: 1. The original method is called with proper arguments 2. The return value maintains the correct type 3. Type information is preserved for method chaining
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Any
|
Positional arguments for the method call |
()
|
**kwargs |
Any
|
Keyword arguments for the method call |
{}
|
Returns¶
The result of the method call, properly typed as the NewType subclass
Source code in newtype/extensions/newtypemethod.pyi
__get__(inst, owner)
¶
Implement the descriptor protocol for method binding.
This method is called when accessing a method on either the class or an instance. It ensures proper method binding and type preservation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inst |
Any | None
|
The instance the method is being accessed from (None for class access) |
required |
owner |
type[Any] | None
|
The class that owns this method |
required |
Returns¶
A bound or unbound NewTypeMethod instance
Source code in newtype/extensions/newtypemethod.pyi
Constants¶
NEWTYPE_INIT_ARGS_STR¶
Internal string constant used to store initialization arguments.
NEWTYPE_INIT_KWARGS_STR¶
Internal string constant used to store initialization keyword arguments.
NEWTYPE_EXCLUDE_FUNC_STR¶
Internal string constant used to mark functions that should not be wrapped.
Type Variables¶
T¶
Type variable used for generic type hints in the implementation.
Logging¶
The library uses Python's standard logging module for debugging and error reporting. The logger name is 'newtype'.